0

I'm currently struggling with a problem I'd like you to help me solving... basically I'm trying to get some images from the internal gallery of the phone in this way

Intent intent = new Intent();

intent.setType("image/*");

intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

but when I get the Uri in the onActivityResult method using this

Uri images_uri = data.getData();

the data is empty (this thing doesn't happen when the image is fetched from the SD card)

How could I solve it?

Moreover, as secondary problem how could I get more than 1 image? I read about using ACTION_SEND_MULTIPLE but this opens a choice for sending methods instead of places from where to fetch images...

thank you in advance

Community
  • 1
  • 1
kioli
  • 635
  • 1
  • 10
  • 26

2 Answers2

0

Try this

    public static final int GALLERY_CODE = 322;
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            GALLERY_CODE);


         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { //Assinging on corresponding import
    super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_CODE && resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        try {
            //add logic for coping file
        } catch (Exception e) {
            e.printStackTrace();
        }
       }

    }

    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);
}
Ranjit
  • 5,130
  • 3
  • 30
  • 66
Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39
  • Nope I already managed to face the problem in this way... the fact is that once you arrive to the line "Uri selectedImageUri = data.getData();" the URI you get is empty because the data you get are empty... – kioli Apr 26 '12 at 11:31
  • if u get the default gallery view to select the picture?\ – Jackson Chengalai Apr 26 '12 at 11:47
  • yes I get the default gallery from where to choose the pics, but when the SD card is not in place, even though the gallery shows (far less pics obviously but still some) and I pick the pic it doesn't give me anything back – kioli Apr 26 '12 at 11:58
  • i think this will help u..http://stackoverflow.com/questions/550905/access-pictures-from-pictures-app-in-my-android-app http://stackoverflow.com/questions/4843764/internal-vs-external-uri-on-fetching-data-from-mediastore – Jackson Chengalai Apr 26 '12 at 12:14
  • mmhh.. hardly think so... I've been through it twice but with no result... I don't know if it could be something in the manifest preventing me to do so... but thank you anyway – kioli Apr 26 '12 at 13:15
0

Well, in my case it was actually a faulty cache list (the gallery was showing photos that weren't actually there, so if I selected them, the actual URI turned out to be nil).

Before you call the ACTION_GET_CONTENT intent, I suggest adding this command:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

which will tell the Gallery it should refresh its data. If you do this, be careful to put a brief sleep in your thread (for instance:

  Thread.sleep(1000);

). This should allow the refresh action to finish before it's actually opened. On my phone, at least, that prevented a brief flash of the Gallery app.

DigCamara
  • 5,540
  • 4
  • 36
  • 47