How to get Bitmap From imageUri in Api level 30?
Have a look at the documentation
This method was deprecated in API level 29.
loading of images should be performed through ImageDecoder#createSource(ContentResolver, Uri), which offers modern features like PostProcessor.
So you have to make use of ImageDecoder.createSource
, like this:
Bitmap bitmap = null;
ContentResolver contentResolver = getContentResolver();
try {
if(Build.VERSION.SDK_INT < 28) {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
} else {
ImageDecoder.Source source = ImageDecoder.createSource(contentResolver, imageUri);
bitmap = ImageDecoder.decodeBitmap(source);
}
} catch (Exception e) {
e.printStackTrace();
}
The above should be thread-safe.
How to convert URI to bitmap?
To convert URI to the bitmap you can do as follows.
try {
if( Uri.parse(paths)!=null ){
Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
}
}
catch (Exception e) {
//handle exception
}
When you are getting file form file picker or camera intent then you can do this.
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);
}
}
Convert Uri to Bitmap
Quoting the documentation for getSavedUri()
:
This field is only returned if the
ImageCapture.OutputFileOptions
is backed byMediaStore
constructed with#Builder(ContentResolver, Uri, ContentValues)
That is not the constructor that you are using for OutputFileOptions
. So, check to see if getSavedUri()
is returning null
. If it is, you will need to use photofile
, including taking steps to save that in the saved instance state Bundle
.
If getSavedUri()
is returning a non-null
value, you might want to edit your question and supply the complete stack trace associated with your crash (rather than using a pastebin).
Kotlin: How to convert image uri to bitmap
Use this:
val imageUri: Uri = intent.data;
val bitmap: Bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver(), Uri.parse(imageUri))
val my_img_view = findViewById(R.id.my_img_view) as Imageview
my_img_view.setImageBitmap(bitmap)
URI to Bitmap C# ASP.NET
[HttpPost]
public JsonResult Scan(string file)
{
byte[] data = Convert.FromBase64String(file.Substring(file.LastIndexOf(',') + 1));
Image image;
Bitmap bitmap;
using (MemoryStream ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
bitmap = new Bitmap(image);
}
}
Related Topics
Android Fragment No View Found For Id
Getactivity() Returns Null in Fragment Function
Handle Textview Link Click in My Android App
Android Studio Suddenly Cannot Resolve Symbols
Android and Xmpp: Currently Available Solutions
Do Fragments Really Need an Empty Constructor
Android Respond to Url in Intent
Android Studio 3.5 Refactor Issue
How to Store Image as Blob in Sqlite & How to Retrieve It
Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Download Binary File from Okhttp
Android - Listen For Incoming Sms Messages
How to Use Prepared Statements in Sqlite in Android
How to Create an Object of an Activity in Other Class
Custom Adapter Getview() Method Is Not Called
How to Turn on Front Flash Light Programmatically in Android