保存图像从Android的drawble资源到SD卡

保存图像从Android的drawble资源到SD卡

本文介绍了保存图像从Android的drawble资源到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何通过点击一个按钮保存图像到用户的SD卡。难道有人告诉我该怎么做。图像是以.png格式,它是存储在可拉伸目录。欲编程按钮来保存该图像到用户的SD卡

I'm wondering how to save an image to user's sdcard through a button click.Could some one show me how to do it. The Image is in .png format and it is stored in the drawable directory. I want to program a button to save that image to the user's sdcard.

推荐答案

保存文件(这是像你的情况)的过程描述如下:

The process of saving a file (which is image in your case) is described here: save-file-to-sd-card

假设你有一个形象,即ic_launcher在你的绘制。然后得到一个位图对象,从这个图像,如:

Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:

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

可以使用检索到的路径SD卡:

The path to SD Card can be retrieved using:

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

然后保存使用按钮点击SD卡:

Then save to sdcard on button click using:

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

不要忘了添加 android.permission.WRITE_EXTERNAL_STORAG​​E 许可。

下面是用于保存从绘制修改后的文件: SaveToSd ,一个完整的示例项目: SaveImage

Here is the modified file for saving from drawable: SaveToSd, a complete sample project: SaveImage

这篇关于保存图像从Android的drawble资源到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:27