How to save a bitmap image with imageview onclick [closed]

↘锁芯ラ 提交于 2019-11-27 21:44:38
Seshu Vinay

To get Bitmap from imageView:

imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();

To save it in a file:

OutputStream fOut = null;
Uri outputFileUri;
try {
    File root = new File(Environment.getExternalStorageDirectory()
        + File.separator + "folder_name" + File.separator);
    root.mkdirs();
    File sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
    Toast.makeText(this, "Error occured. Please try again later.",
    Toast.LENGTH_SHORT).show();
}
try {
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
} catch (Exception e) {
}

You have to

  1. Save the image to your persistent storage.
  2. Add an entry to the MediaStore content provider.

First one can be achieved using the following code:

FileOutputStream out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

Second,

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);

First get the drawingCache(bitmap) of the imageView and then save the bitmap to the SDCard.

File folder = new File(Environment.getExternalStorageDirectory()+"/folder/"); if(!folder.exists()) folderAppointment.mkdirs();

try {
    this.setDrawingCacheEnabled(true);
    FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
    Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache();
    bitmap.compress(CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!