感谢阅读。
[编辑] 如果我省略选项,即
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
然后它返回一个位图。
但是选项作为参数,即。,
bmp = BitmapFactory.decodeFile(picturePath, options);
它返回null。
任何人都可以认为选项缺失或错误的原因是什么?
再次感谢,
[编辑结束]
目标是选择一个图像,根据需要对其进行下采样,保存,然后将其加载到图像视图中。
BitmapFactory解码始终返回null。 我确实在清单中设置了权限,路径显示完整.. /storage/emulated/0/aerg.png
见下文:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
String imageType = options.outMimeType;
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
BitmapFactory.decodeFile(picturePath, options);
int inSampleSize = 1;
if (height > mheight || width > mWidth)
{
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > mheight
&& (halfWidth / inSampleSize) > mWidth)
{
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
Bitmap bmp = BitmapFactory.decodeFile(picturePath,options);
String downsampledPicturePath = saveImage(bmp, picturePath, "PrinterImages");
Bitmap b = BitmapFactory.decodeFile(downsampledPicturePath);
iv.setImageBitmap(b);
//ImageView imageView = (ImageView) findViewById(R.id.theImageView);
//imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
private String saveImage(Bitmap bmp, String path, String folderName)
{
Context context = getApplicationContext();
File folder = context.getDir(folderName, Context.MODE_PRIVATE); //Creating an internal dir;
String pictureName = path.substring(path.lastIndexOf("/") + 1);
String picture = new File(folder, pictureName).getAbsolutePath();
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(picture);
// Use the compress method on the BitMap object to write image to the OutputStream
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e)
{
e.printStackTrace();
}
return picture;
}
答案 0 :(得分:0)
我将从decodeFile文档中的条目开始,因为这显然非常重要;
“
public static Bitmap decodeFile(String pathName,BitmapFactory.Options opts)
在API级别1中添加 将文件路径解码为位图。如果指定的文件名为null,或者无法解码为位图,则该函数返回null。
参数 pathName要解码的文件的完整路径名。 opts null-ok;控制下采样的选项以及图像是否应该被完全解码,或仅返回大小。 返回 解码后的位图,如果图像数据无法解码,则为null;如果opts为非null,则返回请求仅返回大小的opts(在opts.outWidth和opts.outHeight中)
“
我不太明白这个描述,但在我看来,你从未在查看代码时在选项中设置宽度或高度?我可能完全不喜欢这个。
另请注意; http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
“public boolean inJustDecodeBounds如果设置为true,解码器将返回null(无位图),但是out ...”
我在旅途中写下了这个回复。我认为上面的引用是你的问题 - 你在代码中将其设置为true。