【问题标题】:Image download and saving- android图片下载和保存-android
【发布时间】:2013-05-23 10:56:40
【问题描述】:

我在 gridview 中有一些图像。当我单击图像时,图像将在另一个活动中打开。在那个活动中,我有一个保存按钮。单击保存按钮时,图像应下载并保存在 sd 卡中。现在我的问题是如何下载和保存图像。

这是我的代码

主要活动.java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beaches);

        GridView gridView = (GridView) findViewById(R.id.gridView1);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });


        Button home=(Button)findViewById(R.id.button1);
        home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getApplicationContext(),MainActivity.class));

            }
        });
    }

Fullimageactivity.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beachfull);



        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.images[position]);

        Button dwnld = (Button)findViewById(R.id.download);
        dwnld.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });


    }



}

【问题讨论】:

    标签: android gridview android-activity download android-image


    【解决方案1】:

    下面的代码是从一个 URL 保存 ImageFile

    private String path;
    private Bitmap bmImg;
    
    void downloadFile(String fileUrl, String fileName) {
        // here you can add folder in which you want the image to be stored
          path = sdCard.getAbsolutePath() ;
    
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
    
            if (bmImg != null) {
                bmImg.recycle();
            }
    
            bmImg = BitmapFactory.decodeStream(is);
            FileOutputStream out = new FileOutputStream(path + "/" + fileName + ".jpg");
            bmImg.compress(Bitmap.CompressFormat.JPEG, 50, out);
    
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            // Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        } 
    }
    

    记住你还需要AndroidManifest.xml的权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多