kioli
kioli

Reputation: 725

Reading images from the memory of the phone (no SD)

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

Upvotes: 0

Views: 1198

Answers (2)

Jackson Chengalai
Jackson Chengalai

Reputation: 3937

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);
}

Upvotes: 0

DigCamara
DigCamara

Reputation: 5568

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.

Upvotes: 0

Related Questions