许多人之前已经问过这个问题并找到了解决问题的正确方法。但不知怎的,它不适合我。当我尝试选择要显示的大图像时,我的应用程序崩溃了。我试图通过使用BitmapFactory缩放它来减小大小。这不起作用。请帮忙。
我的代码:
public void browse(View v){
@SuppressWarnings("unused")
int id = v.getId();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imgUri = data.getData();
logopath = getPath(imgUri);
//This block is to show the view
try {
if(bitmap != null){
bitmap.recycle();
}
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
iv.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 100, 100, true));
//bitmap = shrinkBitmap(logopath,100,100);
//iv.setImageBitmap(bitmap);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
if (stream != null) {
try {
stream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
private Bitmap shrinkBitmap(String file, int height, int width) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
LogCat详细信息:
06-25 07:16:26.056: E/dalvikvm-heap(1973): Out of memory on a 48000016-byte allocation.
06-25 07:16:26.206: E/AndroidRuntime(1973): FATAL EXCEPTION: main
06-25 07:16:26.206: E/AndroidRuntime(1973): java.lang.OutOfMemoryError
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.example.tg_db1.edit_company.onActivityResult(edit_company.java:113)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.os.Looper.loop(Looper.java:137)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-25 07:16:26.206: E/AndroidRuntime(1973): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 07:16:26.206: E/AndroidRuntime(1973): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 07:16:26.206: E/AndroidRuntime(1973): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
答案 1 :(得分:0)
使用采样技术。采样值越多,图像尺寸越小。 希望您能够处理OutOfMemory错误,这是由于堆大小较小导致较大的图像大小造成的。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 15;
Bitmap bm = BitmapFactory.decodeStream(stream);
如果您使用的是android 3.0,请在清单文件中的应用程序标记内使用android:largeHeap =“true”标记。
application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme>
android:largeHeap="true"