【问题标题】:Android display image from sdcard error [duplicate]Android显示来自sdcard错误的图像[重复]
【发布时间】:2013-08-07 00:40:30
【问题描述】:
08-06 11:35:01.851: E/AndroidRuntime(10736): FATAL EXCEPTION: main
08-06 11:35:01.851: E/AndroidRuntime(10736): java.lang.OutOfMemoryError
08-06 11:35:01.851: E/AndroidRuntime(10736):    at     android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:582)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:380)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:413)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at com.accusol.realestate.property.Property3AddFragmentActivity.onClick(Property3AddFragmentActivity.java:812)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.view.View.performClick(View.java:3558)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.view.View$PerformClick.run(View.java:14157)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.os.Handler.handleCallback(Handler.java:605)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.os.Looper.loop(Looper.java:137)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at android.app.ActivityThread.main(ActivityThread.java:4514)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-06 11:35:01.851: E/AndroidRuntime(10736):    at dalvik.system.NativeStart.main(Native Method)

我使用下面的代码来显示图像

vi = inflator.inflate(R.layout.item_image, null);

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageVIew  imgeview=(ImageVIew )vi.findViewById(R.id.imgItem);
imgeview.setImageBitmap(myBitmap);
vi.setTag(i);

这是在 brimapFactory.decodFile 中崩溃...当有大图像文件时

【问题讨论】:

标签: android android-imageview out-of-memory android-image


【解决方案1】:

使用以下代码重新采样您的图像文件,然后将位图设置为 imageview。

// decodes image and scales it to reduce memory consumption
public static Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

【讨论】:

  • 它工作正常,但图像看起来太模糊
  • @Pintu Corna,通过更改最终的 int REQUIRED_SIZE = 70 来消除图像的模糊性;价值。即检查它的不同值
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 2012-01-06
  • 1970-01-01
相关资源
最近更新 更多