缩小取自 ACTION_GET_CONTENT returns 的图像 null
scaling down an image taken from ACTION_GET_CONTENT returns null
我试图在 ACTION_GET_CONTENT 系统意图中选择图像后,在 onActivityResult 获取图像。我正在使用文档的 'setPic()' 方法来减小图像大小,但由于某种原因,我在使用该方法时什么也没得到。这是我的 Intent、onActivityResulty 和 setPic() 方法 -
private void requestPickingPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_VIDEO_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_VIDEO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri originalDataUri = data.getData();
cachedLocalImageUri = originalDataUri;
Timber.d("Receive image from: %s", originalDataUri);
mImageSent.setImageURI(cachedLocalImageUri);
setPic(originalDataUri.toString(), mImageSent);
}
}
private void setPic(String imagePath, ImageView destination) {
int targetW = destination.getWidth();
int targetH = destination.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = (int) Math.sqrt(Math.min(photoW/targetW, photoH/targetH));
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
destination.setImageBitmap(bitmap);
}
我的目标是使用我从意图中取回的 Uri 以显着降低图像分辨率,以便我可以将它用于大小受限的 firebase 云消息丰富通知。
originalDataUri.toString()
不会给你文件路径。
因此你不能BitmapFactory.decodeFile(imagePath, bmOptions);
。
相反,您可以使用 BitmapFactory.decodeStream(InputStream is)
,一旦您从 Uri
:
打开流
将 setPic
方法的参数 String imagePath
更改为 Uri uri
,而不是
BitmapFactory.decodeFile(imagePath, bmOptions);
这样做:
InputStream is = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, bmOptions);
和方法的最后一样:而不是
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
做
Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions);
请注意,您需要一个有效的 Context
。
我试图在 ACTION_GET_CONTENT 系统意图中选择图像后,在 onActivityResult 获取图像。我正在使用文档的 'setPic()' 方法来减小图像大小,但由于某种原因,我在使用该方法时什么也没得到。这是我的 Intent、onActivityResulty 和 setPic() 方法 -
private void requestPickingPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_VIDEO_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_VIDEO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri originalDataUri = data.getData();
cachedLocalImageUri = originalDataUri;
Timber.d("Receive image from: %s", originalDataUri);
mImageSent.setImageURI(cachedLocalImageUri);
setPic(originalDataUri.toString(), mImageSent);
}
}
private void setPic(String imagePath, ImageView destination) {
int targetW = destination.getWidth();
int targetH = destination.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = (int) Math.sqrt(Math.min(photoW/targetW, photoH/targetH));
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
destination.setImageBitmap(bitmap);
}
我的目标是使用我从意图中取回的 Uri 以显着降低图像分辨率,以便我可以将它用于大小受限的 firebase 云消息丰富通知。
originalDataUri.toString()
不会给你文件路径。
因此你不能BitmapFactory.decodeFile(imagePath, bmOptions);
。
相反,您可以使用 BitmapFactory.decodeStream(InputStream is)
,一旦您从 Uri
:
将 setPic
方法的参数 String imagePath
更改为 Uri uri
,而不是
BitmapFactory.decodeFile(imagePath, bmOptions);
这样做:
InputStream is = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, bmOptions);
和方法的最后一样:而不是
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
做
Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions);
请注意,您需要一个有效的 Context
。