【问题标题】:Android : java.lang.OutOfMemoryError when creating an image gallery applicationAndroid:创建图片库应用程序时出现 java.lang.OutOfMemoryError
【发布时间】:2014-05-03 20:12:51
【问题描述】:

尝试创建一个从手机内存中读取图像的图库应用程序。但是在显示三张图片后,应用程序崩溃了。

代码如下

public class MainActivity extends ActionBarActivity {
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
Gallery gallerry;
GridViewAdapter adapter;
File file;

@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       // Check for SD Card
       if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
              Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                           .show();
       } else {
              file = new File(Environment.getExternalStorageDirectory()
                           + File.separator + "/sdcard/download/media");
              file.mkdirs();
       }

       if (file.isDirectory()) {
              listFile = file.listFiles();
              FilePathStrings = new String[listFile.length];
              FileNameStrings = new String[listFile.length];
              String len=Integer.toString(listFile.length);
              Toast.makeText(this, len, Toast.LENGTH_LONG)
              .show();

              for (int i = 0; i <listFile.length; i++) {
                    FilePathStrings[i] = listFile[i].getAbsolutePath();
                    FileNameStrings[i] = listFile[i].getName();

              }
       }

       gallerry = (Gallery) findViewById(R.id.gridview);
       adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
       gallerry.setSpacing(2);
       gallerry.setAdapter(adapter);

       gallerry.setOnItemClickListener(new OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> parent, View view,
                           int position, long id) {

                    zoomImage(position);
              }

       });
}

private void zoomImage(int position) {
       //final Dialog dialog = new Dialog(MainActivity.this);
       //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
       //dialog.getWindow().setBackgroundDrawableResource(
         //           android.R.color.transparent);
       //dialog.setContentView(R.layout.image_zoomdialog);
       ImageView imageview = (ImageView) findViewById(R.id.imageView1);
       Bitmap bmp = BitmapFactory.decodeFile(FilePathStrings[position]);
       imageview.setImageBitmap(bmp);
       //dialog.show();

}


}

class GridViewAdapter extends BaseAdapter {

   // Declare variables
   private Activity activity;
   private String[] filepath;
   private String[] filename;

   private static LayoutInflater inflater = null;

   public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
          activity = a;
          filepath = fpath;
          filename = fname;
          inflater = (LayoutInflater) activity
                       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   }

   public int getCount() {
          return filepath.length;

   }

   public Object getItem(int position) {
          return position;
   }

   public long getItemId(int position) {
          return position;
   }

   public View getView(int position, View convertView, ViewGroup parent) {

            // TODO Auto-generated method stub
    ImageView i = new ImageView(activity);

    Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
    //i.setImageResource(mImageIds[position]);
    i.setLayoutParams(new Gallery.LayoutParams(200, 200));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setImageBitmap(bmp);
          return i;
   }
}

日志如下

05-03 17:22:04.285:E/AndroidRuntime(2243):致命异常:主要 05-03 17:22:04.285: E/AndroidRuntime(2243): 进程: com.example.gallery, PID: 2243 05-03 17:22:04.285: E/AndroidRuntime(2243): java.lang.OutOfMemoryError 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:369) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:395) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 com.example.gallery.GridViewAdapter.getView(MainActivity.java:155) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.View.layout(View.java:14844) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.FrameLayout.onLayout(FrameLayout.java:388) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.widget.FrameLayout.onLayout(FrameLayout.java:388) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2018) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1775) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.Choreographer.doCallbacks(Choreographer.java:574) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.Choreographer.doFrame(Choreographer.java:544) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.os.Handler.handleCallback(Handler.java:733) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.os.Handler.dispatchMessage(Handler.java:95) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.os.Looper.loop(Looper.java:136) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 android.app.ActivityThread.main(ActivityThread.java:5102) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 java.lang.reflect.Method.invoke(Method.java:515) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 05-03 17:22:04.285: E/AndroidRuntime(2243): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 05-03 17:22:04.285: E/AndroidRuntime(2243): at dalvik.system.NativeStart.main(Native Method)

希望这会有所帮助!

【问题讨论】:

    标签: android out-of-memory


    【解决方案1】:

    可能是您正在加载的图像太大导致堆大小快速增长并导致应用程序崩溃尝试缩小图像大小然后加载它。 您可以使用以下代码:

    public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
    }
    

    【讨论】:

    • 感谢您的回复。我设法降低了画廊和图像视图的图像质量。但是应用程序在第四张图像上崩溃了​​。我给出了非常低的数量作为参数,例如高度和宽度分别为 10 和 10
    • 我猜内存不足异常仍然存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多