简体   繁体   中英

Loading image into bitmap with uri

At first, I get path to image from user's gallery, like that:

@Override 
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent imageReturnedIntent) { 
 super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
 
 switch (requestCode){ 
 case Pick_image: 
 if(resultCode == RESULT_OK){ 
 try { 
 final Uri imageUri = imageReturnedIntent.getData(); 
 final InputStream imageStream = getContentResolver().openInputStream(imageUri); 
 
 pathToImage = imageUri.toString(); 
 choosePath.setImageResource(R.drawable.ic_check); 
 } catch (FileNotFoundException e){ 
 e.printStackTrace(); 
 } 
 } 
 } 
 }

And save it to Database, for example: "content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20210522_105740.jpg", I have xiaomi device. Then in other Activity I need get this image, I do it like that:

bitmap = BitmapFactory.decodeFile(favouriteTemplates.get(position).getImageLink()); 
 holder.icon.setImageBitmap(bitmap);

Where "favouriteTemplates.get(position).getImageLink" is my path. But decodeFile returns null and my ImageView remains empty, although my LogCat doesnt show any exceptions

Try with the following code you do not need to convert into bitmap you can also display image on imageView using imageUri.

String imageUri = favouriteTemplates.get(position).getImageLink();
holder.icon.setImageURI(imageUri)

//Use this for bitmap
Bitmap bitmap = 
MediaStore.Images.Media.getBitmap(this.getContentResolver(), 
imageUri);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM