0

I have this code to pick the images from gallery or camera:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA,
                        MediaStore.Images.Media.DISPLAY_NAME };
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    ext = filePath
                            .substring(filePath.lastIndexOf(".") + 1);
                    photod = BitmapFactory.decodeFile(filePath);

                    new AsyncTaskOne().execute(new String[] {});
    (fileNameIndex);


                }
            }
            break;

        case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
            if (requestCode == CAMERA_REQUEST) {
                photod = (Bitmap) data.getExtras().get("data");
                new AsyncTaskOne().execute(new String[] {});
            }
        }
    }

When I press the confirm button I invoke this listener

OnClickListener confirm = new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent("com.striget.eu.UpdatePhoto");
            i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
            i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
            startActivity(i);
        }
    };

(where codedPhoto is the image coded in base64 by another method) to send the image and its extension in another activity

Everything works fine with small images but if I chose a medium size image or large photo(also if isn't very large), the app freezes, the screen becomes black and If I wait some minutes return on the current activity without showing any error in the stack and the invoked intent PropertiesUpdatedPhoto doesn't start.

How I could fix this problem?

AndreaF
  • 11,975
  • 27
  • 102
  • 168

1 Answers1

1

This problem is discuss here, we should keep intent extra content as small as possible.

My suggestion is instead of passing image, perhaps passing the image URI or Path would be a better solution. Then only load the image in that Activity.

Example :

OnClickListener confirm = new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent("com.striget.eu.UpdatePhoto");
        i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
        // Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
        i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
        startActivity(i);
    }
};
NcJie
  • 812
  • 7
  • 14
  • The image that I must pass to the other activity needs to be coded in the current activity. So: How could I store the coded image in a temp file to pass only the path? – AndreaF Jul 29 '12 at 17:19
  • Yes as you suggested Store the coded image in as temp file and get the temp file Path and pass it to the new Activity. Just curious Why does the image need to be encoded? – NcJie Jul 29 '12 at 17:28
  • I must nest the image in a JSON to send it to a webservice. – AndreaF Jul 29 '12 at 17:39
  • Okay my suggestion is to decode the image first before storing them into image. If the decoded data is in String format. You can do it this way InputStream is = new ByteArrayInputStream(str.getBytes()); and for the rest, you can follow this code snippet here http://stackoverflow.com/a/3296950/1559049 – NcJie Jul 29 '12 at 17:49
  • And you will need to include in your AndroidManifest.xml – NcJie Jul 29 '12 at 17:52