尝试缩放 android 图库中的图像和 return 位图
Trying to scale an image and return a bitmap from android gallery
我目前正在从 android 图库中提取图像,其中 returns 一个 uri 到 activity 进行预览,然后被发送到 Parse 数据库。
发送的图片太大所以我实现了方法found in the android developer documentation
这是我正在使用的东西
Uri selectedImage = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
imageBitmap = getBitmapFromReturnedImage(imageStream, 800, 800);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// LINE 2 imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// byte[] byteArray = stream.toByteArray();
//
// parseFile = new ParseFile("location_image.jpg", byteArray);
mImage.setImageBitmap(imageBitmap);
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
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) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap getBitmapFromReturnedImage(InputStream inputStream, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options);
}
注释"LINE 2",当该部分未被注释时,returns 一个空指针异常,这让我大吃一惊。对该部分进行评论后,当我运行该应用程序时,图像视图看起来只是空的。
有什么线索吗?
在第二次调用decodeStream 之前,您需要在读取边界后返回到InputStream 的开头,否则您将得到无效的Bitmap。最简单的方法就是关闭流并再次打开它。
试试这个代码(注意函数不再是静态的,允许调用 getContentResolver() 并且你传入的是 Uri 而不是 InputStream):
public Bitmap getBitmapFromReturnedImage(Uri selectedImage, int reqWidth, int reqHeight) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(selectedImage);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// close the input stream
inputStream.close();
// reopen the input stream
inputStream = getContentResolver().openInputStream(selectedImage);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options);
}
像这样打电话
Uri selectedImage = data.getData();
imageBitmap = getBitmapFromReturnedImage(selectedImage, 800, 800);
我目前正在从 android 图库中提取图像,其中 returns 一个 uri 到 activity 进行预览,然后被发送到 Parse 数据库。
发送的图片太大所以我实现了方法found in the android developer documentation
这是我正在使用的东西
Uri selectedImage = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
imageBitmap = getBitmapFromReturnedImage(imageStream, 800, 800);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// LINE 2 imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// byte[] byteArray = stream.toByteArray();
//
// parseFile = new ParseFile("location_image.jpg", byteArray);
mImage.setImageBitmap(imageBitmap);
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
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) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap getBitmapFromReturnedImage(InputStream inputStream, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options);
}
注释"LINE 2",当该部分未被注释时,returns 一个空指针异常,这让我大吃一惊。对该部分进行评论后,当我运行该应用程序时,图像视图看起来只是空的。
有什么线索吗?
在第二次调用decodeStream 之前,您需要在读取边界后返回到InputStream 的开头,否则您将得到无效的Bitmap。最简单的方法就是关闭流并再次打开它。
试试这个代码(注意函数不再是静态的,允许调用 getContentResolver() 并且你传入的是 Uri 而不是 InputStream):
public Bitmap getBitmapFromReturnedImage(Uri selectedImage, int reqWidth, int reqHeight) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(selectedImage);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// close the input stream
inputStream.close();
// reopen the input stream
inputStream = getContentResolver().openInputStream(selectedImage);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options);
}
像这样打电话
Uri selectedImage = data.getData();
imageBitmap = getBitmapFromReturnedImage(selectedImage, 800, 800);