【问题标题】:get bitmap information from bitmap stored in drawable folder从存储在可绘制文件夹中的位图中获取位图信息
【发布时间】:2011-11-06 17:20:46
【问题描述】:
我想读取我的可绘制文件夹中的位图并将其存储为位图变量,以便我可以将其设置为背景。最好的方法是使用“文件阅读器”吗?喜欢
Bitmap decodeFile (String pathName) method
或者有没有办法像这样设置它:
Bitmap bmp = R.drawable."bitmapFileName";
(我试过这个但返回一个int,只是想知道我是否在正确的轨道上)
任何帮助都会很棒:)
【问题讨论】:
标签:
android
file
bitmap
drawable
【解决方案1】:
我通常使用 assets 文件夹
InputStream is = parentActivity.getResources().getAssets().open(iconFile);
Bitmap bmp = BitmapFactory.decodeStream(is);
BitmapDrawable bitmapDrawable = new BitmapDrawable(is);
那么就yourView.setBackgroundDrawable(bitmapDrawable);
【解决方案2】:
R.drawable."bitmapFileName" 实际上只是一个整数,因为它是您项目的 R 类中的一个索引(静态整数)(请参阅更多 here)。您可以像这样从资源文件夹中加载位图:
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
我在Android Development Community找到了这个代码。
【解决方案3】:
可以按名称加载可绘制或位图。这是一个例子:
public Drawable getImageByName(String nameOfTheDrawable, Activity a){
Drawable drawFromPath;
int path = a.getResources().getIdentifier(nameOfTheDrawable,
"drawable", "com.mycompany.myapp");
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
drawFromPath = new BitmapDrawable(source);
return drawFromPath;
}
您当然可以返回 Bitmap 而不是可绘制对象。
Drawbale d = getImageByName("mageFileName", this);
【解决方案4】:
/*
* You can get Bitmap from drawable resource id in the following way
*/
BitmapDrawable drawable = (BitmapDrawable)context.getResources()
.getDrawable(drawableId);
bitmap = drawable.getBitmap();