简体   繁体   English

将图像从 Android 上的可绘制资源保存到 sdcard

[英]Save image to sdcard from drawable resource on Android

I'm wondering how to save an image to user's sdcard through a button click.我想知道如何通过单击按钮将图像保存到用户的 SD 卡。 Could some one show me how to do it.有人可以告诉我如何做到这一点。 The Image is in .png format and it is stored in the drawable directory.图像为 .png 格式,并存储在 drawable 目录中。 I want to program a button to save that image to the user's sdcard.我想编写一个按钮来将该图像保存到用户的 SD 卡中。

The process of saving a file (which is image in your case) is described here: save-file-to-sd-card 保存文件(在您的情况下是图像)的过程在此处描述: save-file-to-sd-card


Saving image to sdcard from drawble resource: 从drawble资源将图像保存到SD卡:

Say you have an image namely ic_launcher in your drawable. 假设您的drawable中有一个图像即ic_launcher。 Then get a bitmap object from this image like: 然后从这个图像获取一个位图对象,如:

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

The path to SD Card can be retrieved using: 可以使用以下方法检索SD卡的路径:

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

Then save to sdcard on button click using: 然后使用以下命令保存到按钮单击上的SD卡:

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

Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission. 不要忘记添加android.permission.WRITE_EXTERNAL_STORAGE权限。

Here is the modified file for saving from drawable: SaveToSd , a complete sample project: SaveImage 这是用于从drawable保存的修改文件: SaveToSd ,一个完整的示例项目: SaveImage

I think there are no real solution on that question, the only way to do that is copy and launch from sd_card cache dir like this: 我认为在这个问题上没有真正的解决方案,唯一的方法是从sd_card cache dir复制和启动,如下所示:

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);

If you use Kotlin , you can do like this:如果你使用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)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM