Android 如何使用imageview onclick保存位图图像

Android 如何使用imageview onclick保存位图图像,android,android-gallery,Android,Android Gallery,我正在开发一个android的图像着色。因此,当我单击另一个imageview(如save)时,对图像应用颜色后,我必须将该图像保存到gallery。首先获取imageview的drawingCache(位图),然后将位图保存到SD卡。 文件夹=新文件(Environment.getExternalStorageDirectory()+“/folder/”; 如果(!folder.exists())folderAppoint.mkdirs() 要从imageView获取位图,请执行以下操作: i

我正在开发一个android的图像着色。因此,当我单击另一个imageview(如save)时,对图像应用颜色后,我必须将该图像保存到gallery。

首先获取imageview的drawingCache(位图),然后将位图保存到SD卡。

文件夹=新文件(Environment.getExternalStorageDirectory()+“/folder/”; 如果(!folder.exists())folderAppoint.mkdirs()


要从
imageView
获取位图,请执行以下操作:

imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
要将其保存到文件中,请执行以下操作:

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) {
}
你必须

  • 将映像保存到永久性存储器中
  • 向内容提供程序添加条目
  • 第一个可以使用以下代码实现:

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

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

    谢谢,让我试试这个@Seshu VinayHey vinay i getting error msg“发生错误。请稍后再试。”即使我无法看到在根目录中创建的任何目录??在清单中添加权限:将此行添加到清单文件后,我也会收到相同的错误vinay???@Seshu vinay有用的答案,但你们能告诉我为什么你们要取outputFileUri变量并分配那个值,因为你们并没有使用它吗@让我试试这个@拉古纳贾瓦哈
    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);