user3013172
user3013172

Reputation: 1783

Get image path android

I wanto to load an image into an ImageView on android. Now, i know that if the image is too large, i have to scale it so that it doesn't take too much memory.

I want to do that doint what it says here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Now, the way i get the image is via it's URI like this:

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"text"),1);


protected void onActivityResult(int reqCode, int resCode, Intent data){
    super.onActivityResult(reqCode, resCode, data);

    if (reqCode == 1 && resCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
    }
}

And if i want to use some decode*() method, i cannot do that with the URI. So my frist choice was to use:

decodeFile(imageUri.getPath(),options);

But when i do that, the path i get from the gallery are something like: /documents/image:9023

How should i do this? I just need to open the gallery and get and image from displaying and also be able to save it as a bitmap so i can edit it.

Upvotes: 0

Views: 1019

Answers (2)

Nitin Mali
Nitin Mali

Reputation: 216

Even I was stuck with similar problem few days before,

1.) First convert your Uri to string

String SelectedImageString= selectedImage.toString();

2.)once you will get String path of image you can convert it to bitmap and also resize it using bitmapFactory options.

final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8; 
        Bitmap bitmap;
        bitmap=BitmapFactory.decodeFile(SelectedImageString, options);
        imageView.setImageBitmap(bitmap);

It worked for me I hope it will help you.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006554

And if i want to use some decode*() method, i cannot do that with the URI

Sure you can. Call openInputStream() on ContentResolver, and pass that InputStream to the decodeStream() method on BitmapFactory.

A Uri is not necessarily a file, so do not attempt to treat it as one.

Upvotes: 0

Related Questions