java.lang.OutOfMemory错误 - SD卡图像到ImageView

时间:2015-08-05 06:13:25

标签: java android out-of-memory picasso android-bitmap

我正在尝试将SD卡图像显示到ImageView中并导致OutOfMemory错误。

请告诉我如何将SDCard的图像显示到ImageView中(无论它们有多大。)

我知道我们可以使用这些属性来控制它,但我正在寻找最佳解决方案:

    android:hardwareAccelerated="false"
    android:largeHeap="true"

logcat的:

08-05 11:27:34.530: E/AndroidRuntime(8158): FATAL EXCEPTION: main
08-05 11:27:34.530: E/AndroidRuntime(8158): java.lang.OutOfMemoryError
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.physic.UploadActivity.previewMedia(UploadActivity.java:171)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.physic.UploadActivity.onCreate(UploadActivity.java:109)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.Activity.performCreate(Activity.java:5191)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.os.Looper.loop(Looper.java:137)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.main(ActivityThread.java:4921)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at dalvik.system.NativeStart.main(Native Method)

代码:

/**
     * Displaying captured image/video on the screen
     * */
    private void previewMedia(boolean isImage) {
        // Checking whether captured media is image or video
        if (isImage) {
            imgPreview.setVisibility(View.VISIBLE);

            final Bitmap bitmap = BitmapFactory.decodeFile(filePath);           

            ExifInterface exif = null;
            try {
                exif = new ExifInterface(filePath);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                    ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bmRotated = rotateBitmap(bitmap, orientation);
            imgPreview.setImageBitmap(bmRotated);

        } else {
            imgPreview.setVisibility(View.GONE);
        }

    }

使用Picasso Lib:

        Picasso.with(UploadActivity.this)
        .load(new File(filePath)).rotate(90)
        .fit().centerInside()
        .error(R.drawable.ic_launcher)
        .placeholder(R.drawable.ic_launcher)
        .into(imgPreview);

甚至尝试过这个:

        .transform(transformation)

记录:

Log.d("file:", filePath);
D/file:(6201): /storage/sdcard0/Pictures/Testing/LICPolicy20150805120029.jpg

尝试使用Picasso lib但未显示图像预览..

1 个答案:

答案 0 :(得分:1)

当你的图片太大而你的设备无法渲染时会发生OutOfMemoryException,所以你可以做的是根据你的需要缩小图像,下面是降尺码的代码

 public static Bitmap compressed_Bitmap(Bitmap bitmap) {
    final int maxSize = 700;
    int outWidth;
    int outHeight;
    int inWidth = bitmap.getWidth();
    int inHeight = bitmap.getHeight();
    if (inWidth > inHeight) {
        outWidth = maxSize;
        outHeight = (inHeight * maxSize) / inWidth;
    } else {
        outHeight = maxSize;
        outWidth = (inWidth * maxSize) / inHeight;
    }

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
    return resizedBitmap;
}