如何保存图片?

时间:2016-04-13 11:11:18

标签: android save picasso

我正在使用Picasso来获取图像,我只想保存图像。下面的代码对我来说无法保存图像。

public class DownloadImage extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mindmaps);

    final ImageView imageView = (ImageView) findViewById(R.id.imageView);


    Picasso.with(this)
            .load("http://i.imgur.com/DvpvklR.png")
            .into(imageView);
    final Button btntakephoto = (Button) findViewById(R.id.save);
    btntakephoto.setOnClickListener((View.OnClickListener) this);
}


    public void onClick(View v){
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Drawable image = imageView.getDrawable();
        if (image != null && image instanceof BitmapDrawable) {
            BitmapDrawable drawable = (BitmapDrawable) image;
            Bitmap bitmap = drawable.getBitmap();
            try {
                File file = new File("path where you want to save");
                FileOutputStream stream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.PNG, 100, stream);
                stream.flush();
                stream.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

按下保存按钮图像应保存在图库中。

3 个答案:

答案 0 :(得分:0)

使您的DownloadImage类实现View.OnClickListener接口。

答案 1 :(得分:0)

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

答案 2 :(得分:0)

试试这个,

点击按钮调用此方法:

    a,b,c,d,e,f,g,h,i,j,blank
    A,B,C,D,E,F,G,H,I,J,blank
    1,2,3,4,5,6,7,8,9,0,blank
    Mary,had,a,little,lamb,His,fleece,was,white,as,snow
    And,everywhere,that,Mary,went,The,lamb,was,sure,to,go