Uri Returned After Action_get_content From Gallery Is Not Working In Setimageuri() Of Imageview
I am fetching Uri of a image from gallery using Intent intent = new Intent(); intent.setType('image/*'); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(
Solution 1:
The problem is that you getting the Uri but from that uri you have to create Bitmap to show in your Imageview. There are various mechanism to do the same one among them is this code.
Intentintent=newIntent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Picture"), 1);
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_CANCELED)
{
// action cancelled
}
if(resultCode==RESULT_OK)
{
Uriselectedimg= data.getData();
imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg));
}
}
Solution 2:
Launch the Gallery Image Chooser
Intentintent=newIntent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
PICK_IMAGE_REQUEST is the request code defined as an instance variable.
privateint PICK_IMAGE_REQUEST = 1;
Show Up the Selected Image in the Activity/Fragment
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uriuri= data.getData();
try {
Bitmapbitmap= MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));ImageViewimageView= (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Your layout will need to have an ImageView like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
Post a Comment for "Uri Returned After Action_get_content From Gallery Is Not Working In Setimageuri() Of Imageview"