Android:创建图库应用程序时的java.lang.OutOfMemoryError

时间:2014-05-03 12:06:30

标签: android out-of-memory

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

代码如下

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.galleryy,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方法) 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):at com.example.galleryy.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):at 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):at java.lang.reflect.Method.invokeNative(Native Method) 05-03 17:22:04.285:E / AndroidRuntime(2243):at java.lang.reflect.Method.invoke(Method.java:515) 05-03 17:22:04.285:E / AndroidRuntime(2243):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 05-03 17:22:04.285:E / AndroidRuntime(2243):at 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)

希望这有帮助!

1 个答案:

答案 0 :(得分:0)

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

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;
}