【问题标题】:Save image to sdcard from drawable resource on Android将图像从 Android 上的可绘制资源保存到 sdcard
【发布时间】:2012-05-20 10:41:18
【问题描述】:

我想知道如何通过单击按钮将图像保存到用户的 SD 卡。 有人可以告诉我怎么做。图像为 .png 格式,存储在可绘制目录中。我想编写一个按钮来将该图像保存到用户的 SD 卡中。

【问题讨论】:

  • 什么样的图片?刚拍的照片?来自画廊的照片?完全不同的东西?更多细节确实是必要的。如果您发布了一些相关代码并描述了您尝试过的内容,这将真正有助于我们为您提供帮助。
  • 好的,我添加了详细信息,感谢您的评论
  • 这是一个很好的资源,但有人可以解释一下,比如我在 OnClick 中应该把代码放在哪里?

标签: android drawable sd-card android-image android-button


【解决方案1】:

如果你使用 Kotlin,你可以这样做:

val mDrawable: Drawable? = baseContext.getDrawable(id)
val mbitmap = (mDrawable as BitmapDrawable).bitmap
val mfile = File(externalCacheDir, "myimage.PNG")
        try {
            val outStream = FileOutputStream(mfile)
            mbitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)
            outStream.flush()
            outStream.close()
        } catch (e: Exception) {
            throw RuntimeException(e)
        }

【讨论】:

    【解决方案2】:

    此处描述了保存文件(在您的情况下为图像)的过程:save-file-to-sd-card


    将图片从drawble资源保存到sdcard:

    假设您的可绘制对象中有一个名为 ic_launcher 的图像。然后从此图像中获取位图对象,例如:

    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
    

    可以使用以下方式检索 SD 卡的路径:

    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    

    然后在点击按钮时保存到 sdcard:

    File file = new File(extStorageDirectory, "ic_launcher.PNG");
        FileOutputStream outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    

    别忘了添加android.permission.WRITE_EXTERNAL_STORAGE 权限。

    这是从drawable保存的修改文件:SaveToSd ,完整的示例项目:SaveImage

    【讨论】:

    • 我想从可下载的文件夹中保存图像,而不是从网上!
    • @IranianLIon 这更容易。假设您的可绘制对象中有一个名为 ic_launcher 的图像,然后从该图像中获取一个位图对象,例如:bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); 并使用上面的代码,我将在答案中提供它:)。
    • 谢谢抱歉我迟到了一段时间没登录
    • 我的代码有问题,在 File file = new File(extStorageDirectory, "ic_launcher.PNG"); outStream = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close();所有的输出都有错误你能帮我错误是“outStream无法解析为变量”
    • @IranianLIon 这里是完整的示例项目:Save Image,使用它。
    【解决方案3】:

    我认为这个问题没有真正的解决方案,唯一的方法是从 sd_card 缓存目录复制并启动,如下所示:

    Bitmap bm = BitmapFactory.decodeResource(getResources(), resourceId);
    File f = new File(getExternalCacheDir()+"/image.png");
    try {
        FileOutputStream outStream = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) { throw new RuntimeException(e); }
    
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(f), "image/png");
    startActivity(intent);
    
    
    // NOT WORKING SOLUTION
    // Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + resourceId);
    // Intent intent = new Intent();
    // intent.setAction(android.content.Intent.ACTION_VIEW);
    // intent.setDataAndType(path, "image/png");
    // startActivity(intent);
    

    【讨论】:

      猜你喜欢
      • 2013-07-14
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      相关资源
      最近更新 更多