5

I have some saved images at Pictures folder in SD Card. I want to access those images of my folder directly.

I have used the below code to pick Gallery images directly.

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setDataAndType(Uri.parse("file:///sdcard/Pictures/"), "image/*");

startActivityForResult(intent, 1);

The above code is getting all images from the SD Card. But i need only of my Pictures folder. I also tried Intent.ACTION_GET_CONTENT, the same result.

Please anybody correct me...

Thank you.

1
  • Did you get the answer?, if yes please share.. Commented Nov 20, 2011 at 13:20

1 Answer 1

0

This is the code I use to select a picture from the sd card

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

Note this loads the root folder.

Once a photo is selected the onActivityResult method is called an the image can be gotten.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        if (resultCode == RESULT_OK) {
            if (requestCode == JobActivity.SELECT_PHOTO) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
                getBitmap(selectedImagePath, 0);
                // Log.d("Debug","Saved...." + selectedImagePath);
            }
        }
    } catch (Exception e) {
        Log.e("Error", "Unable to set thumbnail", e);
    }
}

The get Path method

public String getPath(Uri uri) {

    Cursor cursor = null;
    int column_index = 0;
    try {
        String[] projection = { MediaStore.Images.Media.DATA };
        cursor = managedQuery(uri, projection, null, null, null);
        column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
    } catch (Exception e) {
        Log.d("Error", "Exception Occured", e);

    }

    return cursor.getString(column_index);
}

And finally to get the Bitmap

public Bitmap getBitmap(String path, int size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = size;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return bitmap;
}

The size variable allows the scaling of the image by a factor. If you don't wish to scale simply remove the options parameter.

I'm not sure how to tell it to pick from another folder other than the root.

Here is a helpful post too Get/pick an image from Android's built-in Gallery app programmatically

Not the answer you're looking for? Browse other questions tagged or ask your own question.