0

In bello example i am getting arrays as extras and i am i have created one array imagePath[]

which stores paths of all selected images from gridview but my problem is that when i does

it for first image in gridview then it works fine but when i select another image or

public class ShowImages extends Activity {
String title1[],title[],imagePath[];
  Bitmap[] mResources;
                    int cnt=0,j=0;
                    ImageView iv;
                      @Override
                        public void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.layoutxml);
                            iv=(ImageView)findViewById(R.id.imageView1);
                            Bundle extras = getIntent().getExtras();
                            title1 = getIntent().getExtras().getStringArray("imageArray");
                            for(int i=0;i<title1.length;i++){
                                if(title1[i] != null){
                                    cnt++;
                                }
                            }

                            imagePath=new String[cnt];
                            for(int i=0;i<title1.length;i++){
                                if(title1[i] != null){
                                    if(j<cnt){
                                    imagePath[j]=title1[i];
                                    j++;
                                    }
                                }
                            }
                       //  Toast.makeText(getApplicationContext(),String.valueOf(cnt), Toast.LENGTH_LONG).show();
                        mResources=new Bitmap[cnt];     

                        try{
                         for(int i=0;i<cnt;i++)
                         {

                             Toast.makeText(getApplicationContext(),imagePath[i], Toast.LENGTH_LONG).show();
                             File imgFile = new  File(imagePath[i]);
                             if(imgFile.exists())
                             {
                                 mResources[i] = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                                // iv.setImageBitmap(mResources[i]);
                             }
                        }
                        }
                        catch(Exception e){

                        }
                      } 

                }

multiple images in gridview it gives follwing error in logcat

 05-24 17:10:12.616: E/art(3371): Throwing OutOfMemoryError "Failed to    allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM"
 05-24 17:10:12.635: E/AndroidRuntime(3371): FATAL EXCEPTION: main
 05-24 17:10:12.635: E/AndroidRuntime(3371): Process: com.customgallery, PID: 3371
 05-24 17:10:12.635: E/AndroidRuntime(3371): java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM
Darse Rio
  • 26
  • 2
  • First and important question, why are you creating array of Bitmap? for having more information about my question, check this question: [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Paresh Mayani May 24 '15 at 12:06
  • @PareshMayani i have to do it because in php pages i have to send base64 string ..so i need to convert bitmap array to base64 string array – Darse Rio May 24 '15 at 12:10
  • But creating bitmap array would cause the same issue! Rather store the select image URIs/URLs into the string array and convert it into the Bitmap or Base64 while uploading onto the server! – Paresh Mayani May 24 '15 at 12:48
  • @PareshMayani if it is possible to convert path of image int Base64 on server(i mean php pages)could u provide me the code – Darse Rio May 25 '15 at 06:45
  • That's a noob question, how your server would be able to get actual images from your devices from the image URI? – Paresh Mayani May 25 '15 at 06:47

2 Answers2

1

Use a scaled down version of image so that you can avoid memory as well as execution time wastage.

public static Bitmap decodeFile(String photoPath){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);

        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inPreferQualityOverSpeed = true;
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        return BitmapFactory.decodeFile(photoPath, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Visit http://developer.android.com/training/displaying-bitmaps/index.html for more information.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
0

Use UniversalImageLoader Library to eliminating OutOfMemoryError.

Refer this link https://github.com/nostra13/Android-Universal-Image-Loader

Krishna V
  • 1,801
  • 1
  • 14
  • 16