views:

4103

answers:

3

Hi all,

I am trying to open an image / picture in the Gallery built-in app from inside my application.

I have a URI of the picture (the picture is located on the SD card).

Do you have any suggestions?

Thank you in advance.

+1  A: 

Assuming you have an image folder in your SD card directory for images only.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// tells your intent to get the contents
// opens the URI for your image directory on your sdcard
intent.setType("file:///sdcard/image/*"); 
startActivityForResult(intent, 1);

Then you can decide with what you would like to do with the content back in your activity.

This was an example to retrieve the path name for the image, test this with your code just to make sure you can handle the results coming back. You can change the code as needed to better fit your needs.

protected final void onActivityResult(final int requestCode, final int
                     resultCode, final Intent i) {
    super.onActivityResult(requestCode, resultCode, i);

  // this matches the request code in the above call
  if (requestCode == 1) {
      Uri _uri = i.getData();

    // this will be null if no image was selected...
    if (_uri != null) {
      // now we get the path to the image file
     cursor = getContentResolver().query(_uri, null,
                                      null, null, null);
     cursor.moveToFirst();
     String imageFilePath = cursor.getString(0);
     cursor.close();
     }
   }

My advice is to try to get retrieving images working correctly, I think the problem is the content of accessing the images on the sdcard. Take a look at Displaying images on sd card.

If you can get that up and running, probably by the example supplying a correct provider, you should be able to figure out a work-around for your code.

Keep me updated by updating this question with your progress. Good luck

Anthony Forloney
@Anthony, thank you for your response. Unfortunately it doesn't work for me.I get the next error:`android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT typ=file:///sdcard/images/* }`
Michael Kessler
You need to call `startActivityforResult` and supply an activity. That's what I was referring to about deciding on what to next, my bad.
Anthony Forloney
It still doesn't work... I check that the folder exists and that there is an image file inside the folder. I call `startActivityForResult(intent, 1);` and still get this error... This code is located outside the Activity, but I have a reference to the activity and call the `startActivityForResult` method on that reference - maybe this is the reason?
Michael Kessler
Nah, shouldnt be the reason, what is `1` that your passing in? Try `IMAGE_PICK`
Anthony Forloney
The second param is just something for me, isn't it? This is just an int that will be passed back to me together with the result. Tried also the `Intent.ACTION_PICK` instead of `Intent.ACTION_GET_CONTENT`. What do you mean by `IMAGE_PICK`? There is no such a constant. I also tried `intent.setData(Uri.fromFile(new File("/sdcard/image/")));`. I tried all the possible combinations of these and nothing seems to work...
Michael Kessler
yeah `IMAGE_PICK` was a personal constant for another person having problems with Android, I thought it was a constant that choose which image picked, but what happens when you tried to use `intent.setData(...)`, do you get an error message? or nothing happens?
Anthony Forloney
I get an exception on `startActivityForResult` method call in all the cases (`ACTION_GET_CONTENT`, `ACTION_PICK`, `setType`, `setData`), tried to remove the "file://" prefix, tried to remove the "*" posfix, tried to remove both, tried to give the full path to the file - I've tried all the combinations. Notice that it opens the Gallery app when I don't mention the exact folder or file and use the `setType("image/*");`.
Michael Kessler
Does it work for you? Have you tried it? If it does then what platform are you working on? I use the Android 1.5
Michael Kessler
I have not tried it, so I couldn't say whether it would work or not and I am using the Android 2.0
Anthony Forloney
Thank you anyway...
Michael Kessler
Not a problem, im sorry I couldn't help answer your question but I wish you the best of luck in your search
Anthony Forloney
+12  A: 

This is a complete solution

public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

hcpl
A: 

I'm missing something here and can't the example to work. I'm actually more interested in taking a local drawable resource in my app and opening that image in the Gallery app.

Is it necessary to stage that image on the SD card prior to opening it in the Gallery? How would that be accomplished.

As it stands now, when I execute the click, I'm presented with the Gallery app with the original images that were previously stored there. I was expecting to see the image I was attempting to pass...

What am I missing?

Chris
Is it an answer? If not then please add comment to the question or to some answer instead.
Michael Kessler