Question

Trying to create an image gallery application which reads image from the phone memory. But after displaying three pictures, the application crashes.

The code is as follows

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

The Log is as Follows

05-03 17:22:04.285: E/AndroidRuntime(2243): FATAL EXCEPTION: main 05-03 17:22:04.285: E/AndroidRuntime(2243): Process: 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): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:369) 05-03 17:22:04.285: E/AndroidRuntime(2243): at 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): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.View.layout(View.java:14844) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): at 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): at android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.View.layout(View.java:14857) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewGroup.layout(ViewGroup.java:4641) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2018) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1775) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.Choreographer.doFrame(Choreographer.java:544) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.os.Handler.handleCallback(Handler.java:733) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.os.Handler.dispatchMessage(Handler.java:95) 05-03 17:22:04.285: E/AndroidRuntime(2243): at android.os.Looper.loop(Looper.java:136) 05-03 17:22:04.285: E/AndroidRuntime(2243): at 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)

Hope this helps!

Était-ce utile?

La solution

May be the image you are loading is too large that it causing the heap size to grow rapidly and causing App to be crashed try to Down size the Image and then load it. You can use the following code :

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top