感謝您的閱讀。BitmapFactory.Decode返回null
[編輯] 如果我省略的選項,即,
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
然後,它返回一個位圖。
但與選項的說法,即
bmp = BitmapFactory.decodeFile(picturePath, options);
返回null。
任何人都可以理解缺失或錯誤的選項?再次
謝謝,
[EDIT的端]
的目標是選擇圖像,如果需要的話它下采樣,保存它,然後將其加載到一個ImageView的。
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;
}
我已經在這幾天了,任何幫助表示讚賞! – Shaare
好吧,我會paypal星巴克給任何人誰可以幫我解決這個問題。 – Shaare