19

I have included 'share via myApp' option. I inserted following code in the receiving activity class.

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // Figure out what to do based on the intent type
    if (intent.getType().indexOf("image/") != -1) {
        // Handle intents with image data ...
}

What is the next step to retrieve bitmap image.

Robert
  • 343
  • 1
  • 3
  • 14

8 Answers8

40

As you have already get the Uri. Now you have to pass that Uri in getBitmap() to get bitmap and use that bitmap.

Uri imageUri = intent.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);
my_img_view.setImageBitmap(bitmap);
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
9

For getting bitmap from uri,

Bitmap  mBitmap = Media.getBitmap(this.getContentResolver(), uri);

Hope this helps you.

nikvs
  • 1,090
  • 5
  • 9
3
Retrive bitmap from uri.....

public static Bitmap decodeUriToBitmap(Context mContext, Uri sendUri) {
        Bitmap getBitmap = null;
        try {
            InputStream image_stream;
            try {
                image_stream = mContext.getContentResolver().openInputStream(sendUri);
                getBitmap = BitmapFactory.decodeStream(image_stream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getBitmap;
    }
Praful Parmar
  • 213
  • 2
  • 6
1

This is work for me

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
    Uri imageUri = data.getData();
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
0

You can try this. You can call setPic() in onActivityResult method. I have used this in an application to take photos an put it in an ImageView.

private void setPic() {

    //currentPhotoPath contains path of image file.
    //visorFoto is a reference to an ImageView object.
    File file = new File(currentPhotoPath);
    Uri imageUri = Uri.fromFile(file);
    visorFoto.setImageURI(imageUri);

}
Jose
  • 21
  • 4
0

Once u have the Uri, you can use something like:-

fun getBitmapFromUri(context: Context, uri: Uri): Bitmap? {
        
        var parcelFileDescriptor: ParcelFileDescriptor? = null
        return try {
            parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, "r")
            val fileDescriptor: FileDescriptor? = parcelFileDescriptor?.fileDescriptor
            val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
            parcelFileDescriptor?.close()
            image
        } catch (e: Exception) {
            //Log.e(TAG, "Failed to load image.", e)
            null
        } finally {
            try {
                parcelFileDescriptor?.close()
            } catch (e: IOException) {
                e.printStackTrace()
                //Log.e(TAG, "Error closing ParcelFile Descriptor")
            }
        }
    }
Prateek Kumar
  • 321
  • 1
  • 3
  • 16
0

referring to Android Developers documentation you can use this to get bitmap from an URI

    ....

    Uri uri = intent.getData();
    try {
        Bitmap bitmap = getBitmapFromUri(uri);
        // do something with this bitmap

    } catch (IOException e) {
        e.printStackTrace();
    }
}


private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}
Faisal Khan
  • 548
  • 3
  • 16
-5

Please prefer this link.

This is what you are looking for How to get Bitmap from an Uri?

Try this it works for me:

public static Bitmap getBitmapFromURL(String src) {
        try {
            System.out.printf("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            System.out.printf("Bitmap", "returned");
            myBitmap = Bitmap.createScaledBitmap(myBitmap, 100, 100, false);//This is only if u want to set the image size.
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.printf("Exception", e.getMessage());
            return null;
        }
Community
  • 1
  • 1
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • Still tries to fetch from Url, it's URI @nikvs has the cleanest solution (using `getBitmap` helper method from MediaStore class) – Magnus Dec 26 '13 at 11:39