0

In My Android Camera Application, I am using this code to get Select Image from the Android Gallery.

Intent intent = new Intent(); 
                intent.setType("image/*"); 
                intent.setAction(Intent.ACTION_GET_CONTENT);// 
                //startActivity(intent);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
                //finish();

I got the Gallery and all seems fine. but while i select on perticular image on the Gallery then nothing is happend and the activity of gallery get finish.

I want to Select that image and do some operation on it. How it is possible ??

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Just look at the similar question on SO [how to pick a image from gallery (SD Card) for my app in android?](http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android). – user370305 Nov 01 '11 at 06:46

3 Answers3

2

do it inside onActivityResult . here Uri will be the Uri of selected image .

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
2

As you are starting the ActivityForResult you need to write onActivityResult and you can get the output of the called intent in that like this,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {    
        Uri contentUri = data.getData();

        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String tmppath = cursor.getString(column_index);


        Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
        iv.setImageBitmap(croppedImage);    //set to your imageview     
    }
}
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
2
   private static final int GALLERY_PICK = 0;

       /**
     * Start gallery pick activity.
     */
    protected void startGalleryPickActivity() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.putExtra("crop", "true");
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
        photoPickerIntent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());
        startActivityForResult(photoPickerIntent, GALLERY_PICK);
    }

    /**
     * Gets the temp uri.
     * 
     * @return the temp uri
     */
    private Uri getTempUri() {
        return Uri.fromFile(getTempFile());
    }

    /**
     * Gets the temp file.
     * 
     * @return the temp file
     */
    private File getTempFile() {
        if (isSDCARDMounted()) {
            imagePath = Environment.getExternalStorageDirectory() + "/temp.jpg";

            File f = new File(Environment.getExternalStorageDirectory(),
                    "temp.jpg");
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return f;
        } else {
            return null;
        }
    }


       protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {

        switch (requestCode) {

        case GALLERY_PICK:

            if (resultCode == RESULT_OK) {
                //Write your code here
            }

            break;

        default:
            break;

        }
    }

Try using this code.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Kannan Suresh
  • 4,573
  • 3
  • 34
  • 59