Ответ 1
Попробуйте использовать BitmapFactory для устранения проблемы http://developer.android.com/reference/android/graphics/BitmapFactory.html
Мой код:
public Bitmap loadPhoto(Uri uri) {
Bitmap scaled = null;
try {
scalled = Bitmap.createBitmap(
MediaStore.Images.Media.getBitmap(getContentResolver(), uri),
0,0,90, 90);
if (scaled == null) { return null; }
} catch(Exception e) { }
return scaled;
}
После этого. Я показываю масштабирование в ImageView. Каждое изображение поступает с камеры устройства.
Каждый раз, когда я показываю три фотографии с камеры, я получаю сообщение об ошибке: из памяти. Как это решить?
Попробуйте использовать BitmapFactory для устранения проблемы http://developer.android.com/reference/android/graphics/BitmapFactory.html
Ответ правин Катхи всегда вернет ноль. Вот обновленный ответ.
Вот хитрость, закрывайте входной поток после каждого использования. Входной поток означает, что будет использоваться один раз. Для получения дополнительной информации, пожалуйста, следуйте этому ответу
private 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 decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException {
Bitmap bitmap = null;
try {
// Get input stream of the image
final BitmapFactory.Options options = new BitmapFactory.Options();
InputStream iStream = context.getContentResolver().openInputStream(imageUri);
// First decode with inJustDecodeBounds=true to check dimensions
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(iStream, null, options);
if (iStream != null) {
iStream.close();
}
iStream = context.getContentResolver().openInputStream(imageUri);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(iStream, null, options);
if (iStream != null) {
iStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Метод MediaStore.getBitmap - это метод удобства, который не определяет размер выборки при получении растрового изображения. Если вы используете getBitmap (ContentResolver, Uri) и хотите использовать размер выборки, просто используйте ContentResolver для получения входного потока и декодируйте растровое изображение, как обычно (сначала вычисляйте размер выборки, а затем загрузите его соответствующим размер образца).
Для тех, кто ищет образец кода:
private 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 decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException {
// Get input stream of the image
final BitmapFactory.Options options = new BitmapFactory.Options();
InputStream iStream = context.getContentResolver().openInputStream(imageUri);
// First decode with inJustDecodeBounds=true to check dimensions
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(iStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(iStream, null, options);
}