android - 如何将图像从 drawable 传输到 sdcard 中的文件夹?

标签 android image drawable

有没有办法将图像从 res/drawable 文件夹传输到 sdcard 中的文件夹?仍然了解图片的质量和细节?

最佳答案

你必须使用 BitmapFactory.decodeResource() ,通过将项目的资源 ID 传递给它,首先生成一个 Bitmap

Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mybitmap);

然后,您可以使用 answer in this thread 将其保存到存储卡中.

像这样:

try {
    FileOutputStream out = new FileOutputStream(filename);
    myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
    e.printStackTrace();
}

注意:@RaghavSood 在他的评论中是正确的;你真的没有理由需要这样做。您可以在运行时加载可绘制资源,而不是占用存储卡上的空间。

关于android - 如何将图像从 drawable 传输到 sdcard 中的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12499288/

相关文章:

android - getReadableDatabase 经常但不总是返回 null

android - 根据 Random Textview 显示文本

java - 使用首选 DateFormat 格式化 JodaTime DateTime

javascript - 如何使用 javascript/php 自动搜索 `<img src=` [image] 的目录?

image - 将图像或图标添加到 primefaces 选项卡标题

Android可绘制资源: Dynamically change an icon color/"background"?

android - 以编程方式绘制 LayerDrawable

android - Activity 完成后停止android中的振动

jquery - 将图像添加到 <tspan>

java - 动态合并背景