Android BitmapFactory.decodeFile()在某些设备中返回null
我正在使用来自的以下代码。我想压缩图像Android BitmapFactory.decodeFile()在某些设备中返回null,android,bitmap,bitmapfactory,image-compression,Android,Bitmap,Bitmapfactory,Image Compression,我正在使用来自的以下代码。我想压缩图像 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(filePath, options); int actualHeight = options.outHeight; int actualWidth = options.outWidth;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
在从照相机、多媒体资料和照片中选择图像后,我会根据操作系统类型和设备型号在不同的设备中获得不同类型的路径。比如:
1) /storage/emulated/0/Android/data/
2) /raw//storage/emulated/0/mb/1511172993547.jpg
3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582
若路径类似于第一个url,则此代码工作正常。但是如果我得到其他类型的图像,那么BitmapFactory.decodeFile()会给出null
是否有任何方法可以在所有类型的设备和操作系统版本中压缩映像
更新:
要打开选择器,请执行以下操作:
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 1001);
选择图像后:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri imgUri = Uri.fromFile(new File(data.getData().getPath()));
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
dlgImageToPost.setImageBitmap(cmpBitmap);
...
}
对于压缩:
public static Bitmap compressUriQuality(Context mContext, Uri selectedImage) {
InputStream imageStream = null;
Bitmap bmp = null;
try {
imageStream = mContext.getContentResolver().openInputStream(
selectedImage);
bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 50, stream);
if (imageStream != null)
imageStream.close();
stream.close();
stream = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
我也有同样的问题,请向你的应用程序添加存储权限 查看此链接了解更多信息 Tv 2)/raw//storage/simulated/0/mb/1511172993547.jpg 如果用户从“多媒体资料”或“照片”中选择了图片,则不会获得该路径。当然,它不是可用于.decodeFile()的文件系统路径。你是怎么得到的
3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582
这不是有效的内容方案路径。你是怎么得到的?当然不能用于.decodeFile()
如果你“表现正常”,你永远不会得到这样的途径。那么你在做什么呢?基本uri处理错误
using following code from here.
正如您现在所看到的,这是一个相当脏的示例代码
any way to compress image in all types of devices and OS versions.
当然。只需直接使用获得的选定uri。为其打开一个InputStream,并改用.decodeStream()
天哪。。您没有直接使用uri
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
改为
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, data.getData());
您有image
Uri
,所以打开它的InputStream
并使用BitmapFactory
对其进行解码-您可以使用ContentResolver
获取InputStream
。您是否试图从galley或任何其他路径获取图像路径???不,请勿使用任何“图像路径”-您只需要Uri
谢谢您的评论,我会尝试使用Uri感谢您的回复,我会尝试使用UrlError来使用Uri,当我从Xiomi中的照片中选择图像时,data.getData().getPath()返回类似“/-1/1”的url/content://media/external/images/media/7741/ORIGINAL/NONE/1544403096'. 在本例中,mContext.getContentResolver().openInputStream(selectedImageUri)正在提供FileNotFoundException。请显示完整的代码。把代码放在你的帖子里。不在评论中。不可读。同时发布来自logcat的完整异常消息。内容方案位于data.getData().toString()中。不要使用getPath()。还要显示获取uri的目的。您没有回答:您是如何获得它的?
。
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, data.getData());