How to Pick an Image from Gallery (Sd Card) For My App

How to pick an image from gallery (SD Card) for my app?

Updated answer, nearly 5 years later:

The code in the original answer no longer works reliably, as images from various sources sometimes return with a different content URI, i.e. content:// rather than file://. A better solution is to simply use context.getContentResolver().openInputStream(intent.getData()), as that will return an InputStream that you can handle as you choose.

For example, BitmapFactory.decodeStream() works perfectly in this situation, as you can also then use the Options and inSampleSize field to downsample large images and avoid memory problems.

However, things like Google Drive return URIs to images which have not actually been downloaded yet. Therefore you need to perform the getContentResolver() code on a background thread.


Original answer:

The other answers explained how to send the intent, but they didn't explain well how to handle the response. Here's some sample code on how to do that:

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

switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();


Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}

After this, you've got the selected image stored in "yourSelectedImage" to do whatever you want with. This code works by getting the location of the image in the ContentResolver database, but that on its own isn't enough. Each image has about 18 columns of information, ranging from its filepath to 'date last modified' to the GPS coordinates of where the photo was taken, though many of the fields aren't actually used.

To save time as you don't actually need the other fields, cursor search is done with a filter. The filter works by specifying the name of the column you want, MediaStore.Images.Media.DATA, which is the path, and then giving that string[] to the cursor query. The cursor query returns with the path, but you don't know which column it's in until you use the columnIndex code. That simply gets the number of the column based on its name, the same one used in the filtering process. Once you've got that, you're finally able to decode the image into a bitmap with the last line of code I gave.

Exception picking a image from gallery (SD Card) for use on my app... ¿why?

I just dumped your code into my phone and it works just fine. I also tried 5.5 megabyte images without a problem.

I guess what I would recommend is setting up an SD card on your emulator. There are plenty of tutorials on how to do this on the internet. Once you set up the SD card on the emulator, you will be able to get the exception in LogCat and determine where you are going wrong.

android select image from sdcard

I think this is what you are looking for

if (Environment.getExternalStorageState().equals("mounted")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(
Intent.createChooser(
intent,
"Select Picture:"),
requestCode);
}

and to handle the callback

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Bitmap photo = getPreview(selectedImagePath);
}


public String getPath(Uri uri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(proj[0]);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}

public Bitmap getPreview(String fileName) {
File image = new File(fileName);

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / 64;
return BitmapFactory.decodeFile(image.getPath(), opts);
}

hope it helps

Picking images from gallery (SD Card) on my app… ¿How to allow only max 500kb photos?

Can't you just use some standard Java to get the file size, then check against your limit?

private long getFileSize(String path) {
File file = new File(path);
if (file.exists() && file.isFile()) {
return file.length();
} else {
return 0L;
}
}

How to pick image from Gallery only

Your code is correct one, but you need little help from me. Just looks at link it will help you to handle Gallery Link

For your task, you can add this sample code

Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

Exception picking a image from gallery (SD Card) for use on my app... java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Bitmap.createScaledBitmap method as long as I have researched has a highpoint of memory consumption and produces that error, but, I'm not totally sure about it. Anyway it is JAVA API and you cannot go deeper.

You can do the next:

//create a Drawable with your image as parameter
BitmapDrawable d= new BitmapDrawable(youBitmap);
//define bounds for your drawable
int left =0;
int top = 0;
int right=80;
int bottom=80;

Rect r = new Rect(left,top,right,bottom);
//set the new bounds to your drawable
d.setBounds(r);
//set the drawable as view of your image view
profileImage.setImageDrawable(d);

I have not tested this code, but it should work.



Related Topics



Leave a reply



Submit