Android 从drawable文件夹获取文件路径
我想重新调整位图图像的大小。。。因此,我使用下面的代码Android 从drawable文件夹获取文件路径,android,Android,我想重新调整位图图像的大小。。。因此,我使用下面的代码 BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; int scaleFactor = Math.min(photoW / 100,
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW / 100, photoH / 100);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
但我的问题是,我从这样的可绘制文件夹中获取图像
Bitmap icon = BitmapFactory.decodeResource(getResources(),
Const.template[arg2]);
那么,如何将这些内容转换为文件路径,以便在下面的行中进行设置
Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
并且可以获得可调整大小的图像我不明白,为什么要使用
decodeFile()
?我很肯定你可以用
BitmapFactory.decodeResource(getResources(), Const.template[arg2]), bmOptions)
答案如下
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// If set to true, the decoder will return null (no bitmap), but
// the out... fields will still be set, allowing the caller to
// query the bitmap without having to allocate the memory for
// its pixels.
bmOptions.inJustDecodeBounds = true;
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / 100, photoH / 100);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Const.template[arg2],bmOptions);
Drawable draw = new BitmapDrawable(getResources(), bitmap);
/* place image to textview */
TextView txtView = (TextView) findViewById(R.id.imgChooseImage);
txtView.setCompoundDrawablesWithIntrinsicBounds(draw, null,
null, null);
position = arg2;
}
});
public void onItemClick(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
//如果设置为true,解码器将返回null(无位图),但
//out…字段仍将被设置,允许调用方
//查询位图而不必为其分配内存
//它的像素。
bmOptions.inJustDecodeBounds=true;
int photoW=bmOptions.outWidth;
int photoH=bmOptions.outHeight;
//确定要缩小图像的比例
int scaleFactor=Math.min(照片W/100,照片H/100);
//将图像文件解码为位图大小以填充视图
bmOptions.inJustDecodeBounds=false;
bmOptions.inSampleSize=scaleFactor;
bmOptions.inpurgable=true;
位图位图=BitmapFactory.decodeResource(getResources(),常量模板[arg2],bmOptions);
Drawable draw=新的BitmapDrawable(getResources(),位图);
/*将图像放置到文本视图*/
TextView txtView=(TextView)findViewById(R.id.imgChooseImage);
txtView.SetCompoundDrawableSwithinInstincBounds(draw,null,
空,空);
位置=arg2;
}
});