有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android BitmapFactory。解码一个错误

我正在设计一个展示一些图片的活动。下面的代码获取图像文件并将其放置在屏幕上

        for(int i=0;i<photoPaths.size();i++){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            //Bitmap bm= BitmapFactory.decodeFile(new File(photoPaths.get(i)).getAbsolutePath());
            Bitmap bm= BitmapFactory.decodeFile(new File(photoPaths.get(i)).getAbsolutePath());
            int imageH=bm.getHeight();
            int imageW=bm.getWidth();
            ImageView image=new ImageView(this);
            image.setImageBitmap(bm);
            int padding=10;
            image.setPadding(0, padding, padding, 0);
         }

代码在放置5张照片时运行良好。当第六次放置代码失败时,在它们之后

以下是LogCat消息:

       06-27 11:13:13.202: D/skia(17373): --- decoder->decode returned false
       06-27 11:13:13.202: D/AndroidRuntime(17373): Shutting down VM
       06-27 11:13:13.202: W/dalvikvm(17373): threadid=1: thread exiting with uncaught exception (group=0x4142c2a0)
       06-27 11:13:13.202: E/AndroidRuntime(17373): FATAL EXCEPTION: main
       06-27 11:13:13.202: E/AndroidRuntime(17373): java.lang.OutOfMemoryError
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.graphics.BitmapFactory.nativeDecodeStream(Native Method)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.graphics.BitmapFactory.decodeFile(BitmapFactory.java:451)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at .PhotoGallery.onCreate(PhotoGallery.java:94)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.Activity.performCreate(Activity.java:5188)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.ActivityThread.access$700(ActivityThread.java:140)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.os.Looper.loop(Looper.java:137)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at 安卓.app.ActivityThread.main(ActivityThread.java:4921)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at java.lang.reflect.Method.invokeNative(Native Method)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at java.lang.reflect.Method.invoke(Method.java:511)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       06-27 11:13:13.202: E/AndroidRuntime(17373):     at dalvik.system.NativeStart.main(Native Method)

我怎样才能解决这个问题


共 (2) 个答案

  1. # 1 楼答案

    您没有将options对象传递到decodeFile:

     Bitmap bm= BitmapFactory.decodeFile(new File(photoPaths.get(i)).getAbsolutePath(), options);
    
  2. # 2 楼答案

      06-27 11:13:13.202: E/AndroidRuntime(17373): FATAL EXCEPTION: main
       06-27 11:13:13.202: E/AndroidRuntime(17373): java.lang.OutOfMemoryError
    

    你的记忆用完了。您需要减少应用程序的内存使用。请注意,如果图像相当大(比如30+MB),单是图像本身就可能会占用太多内存