Jas*_* O. 1 android bitmapfactory
我已经尝试了我在stackoverflow上找到的所有解决方案来解决这个问题,但是徒劳无功.在运行以下代码时,mAlbumPhotoUri是"/mnt/sdcard/photo/1342147146535.jpg",它是Uri类型.file.exists()表示此文件存在,但在执行最后一行代码后resultBitmap为null.
我究竟做错了什么?
File file = new File(mAlbumPhotoUri.toString());
if(file.exists()){
Toast.makeText(this, "File exists in /mnt", Toast.LENGTH_LONG);}
else {
Toast.makeText(this, "File NOT exists in /mnt", Toast.LENGTH_LONG);}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //only get the size of the bitmap
if (resultPhotoBitmap != null) {
resultPhotoBitmap.recycle();
}
String fname=new File(mAlbumPhotoUri.toString()).getAbsolutePath();
resultPhotoBitmap = BitmapFactory.decodeFile(fname, options);
Run Code Online (Sandbox Code Playgroud)
添加选项inJustDecodeBounds到您BitmapFactory做这一点 ; 它只解码大小Bitmap并将数据加载回选项对象outHeight和outWidth.它不解码实际Bitmap并将其返回给您.
如果您想真正获得Bitmap自己,请删除该选项并decodeFile()再次调用.