Load image in bitmap from Uri in android Q - java

There are similar questions but with deprecated code for android Q...
I am taking and storing photos in a custom folder, for android Q I use:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getActivity().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, namePhoto() + ".jpg");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Folder");
imagenUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
MyPreferences.setPhotoUri(imagenUri.toString(), getContext());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imagenUri);
}
startActivityForResult(takePictureIntent, 199);
}
setPhotoUri stores this content://media/external/images/media/201,
I use the uri to show the photo:
imageView.setImageURI(Uri.parse(MyPreferences.getPhotoUri(getContext())));
the problem is that I need to show a lot of photos and I understand that setImageURI can consume a lot of memory and I can't load the image with Bitmap, I only want to show thumbnails, everything I find on the internet is deprected, how can i do it in Android Q?

Related

how to save high quality image to phone gallery by camera intent

I am trying to develop app which save image to gallery.
I want to call the camera through intent, capture images, and save it locally in the gallery
but the issue is image quality is very poor. can anyone help me figure out why?
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
someActivityResultLauncher.launch(cameraIntent);}
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == RESULT_OK) {
bitmap = (Bitmap) Objects.requireNonNull(result.getData()).getExtras().get("data");
}
imageView.setImageBitmap(bitmap);
saveimage(bitmap);
}
private void saveimage(Bitmap bitmap){
Uri images;
ContentResolver contentResolver = getContentResolver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
images = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
}else {
images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, System.currentTimeMillis() +".jpg");
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "images/*");
Uri uri = contentResolver.insert(images, contentValues);
try {
OutputStream outputStream = contentResolver.openOutputStream(Objects.requireNonNull(uri));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
Objects.requireNonNull(outputStream);
//
}catch (Exception e){
//
e.printStackTrace();
}
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if you get image from data directly, it's in bad quality
Your code is the legacy equivalent of using ActivityResultContracts.TakePicturePreview. It is specifically set up to return a low-resolution bitmap, suitable for a preview.
Instead, use ActivityResultContracts.TakePicture, and supply your destination Uri as part of the contract.
See this and this for more.

Edited bitmap only saving drawn changes without original picture in shared storage and empty bitmap in internal storage

I was adding a new feature to my drawing app project where users could open any picture from the phone gallery and draw on it. But I am having issues saving the new bitmap. Here are the details below :
My app has 3 activities :
Main Activity as a menu,
RecyclerView activity that acts as a gallery,
Drawing activity where users draw
Up until recently whenever a user saved an image, it would create two copies of it. One private that would be stored in application's personal storage that is not available to phone's gallery to use but is displayed in applications own gallery activity, and one public picture in media which will be displayed in the phones gallery. My gallery class checks the internal folder of the application and lists all files in it to display, that is why I also save in private app directory too. Save image method has two sections that handles the scoped storage issues with api 28+ and <28 respectively.
Recently I added a feature where users can pick any picture from phone's gallery and edit it (both images that app created and those that it did not).It uses intent picker function.
Expected results are :
Open a picture from the gallery in the drawing activity and draw on top of it
Save edited version as a new image both privately in app's storage and a public shared copy in phones main gallery
Current issues :
Image opens and is drawn upon without problems (both images made by the app and foreign pictures)
Saving images causes the app to save an empty bitmap in phones private gallery and changes (edits drawn) in shared storage on the phone without actual image that was meant to be edited.
Here is the code :
From MainActivty, code that opens the images from phone's gallery in order to edit them.
// checks permissions for reading external and scoped storage before opening image picker
private void pickAnImage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "SDK >= Q , requesting permission");
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "SDK < Q , requesting permission");
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
} else {
Log.d(TAG, "Permission exists, starting pick intent");
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, IMAGE);
}
}
// handles results from permissions and begins intent picker
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1000) {
Log.d(TAG, "result code good");
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Permission is granted, starting pick");
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, IMAGE);
} else {
Log.d(TAG, "While result code is good, permission was denied");
Toast.makeText(MainActivity.this, "Permission Denied !", Toast.LENGTH_SHORT).show();
}
}
}
// handles results from intent picker and feeds the image path to the drawing activity for image editing
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == IMAGE && data != null) {
Log.d(TAG, "Data from picked image is not null, creating uri and path");
Uri selectedImageUri = data.getData();
String picturePath = getPath(getApplicationContext(), selectedImageUri);
// Log.d("Picture Path", picturePath);
if (picturePath != null) {
Log.d(TAG, "Path creating success, calling art activity");
Intent intent = new Intent(getApplicationContext(), ArtActivity.class);
intent.putExtra("image", picturePath);
startActivity(intent);
} else {
Log.d(TAG, "Path was null");
finish();
}
}else{
Log.d(TAG, "Data seems to be null, aborting");
}
}
// obtains the path from the picked image
private static String getPath(Context context, Uri uri) {
String result = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(proj[0]);
result = cursor.getString(column_index);
Toast.makeText(context, "" + result, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
else {
Toast.makeText(context.getApplicationContext(), "Failed to get image path , result is null or permission problem ?", Toast.LENGTH_SHORT).show();
result = "Not found";
Toast.makeText(context, "" + result, Toast.LENGTH_SHORT).show();
}
return result;
}
Below are methods that handle bitmap editing process :
// Inside initialization method that sets up paint and bitmap objects before drawing
if (bitmap != null) {
// if bitmap object is not null it means that we are feeding existing bitmap to be edited, therefore we just scale it to screen size before giving it to canvas
loadedBitmap = scale(bitmap, width, height);
} else {
// bitmap fed to this method is null therefore it means we are not editing existing picture so we create a new object to draw on
this.bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888, true);
}
Inside onDraw method that handles drawing as it happens :
if (loadedBitmap != null) {
// loadedBitmap not being null means we are editing existing image from above, we previously scaled it so now we feed it to the canvas for editing
canvas.drawBitmap(loadedBitmap, 0, 0, paintLine);
canvas.clipRect(0, 0, loadedBitmap.getWidth(), loadedBitmap.getHeight());
} else {
// we are not editing an image so we draw new empty bitmap to use
canvas.drawBitmap(bitmap, 0, 0, tPaintline);
}
And finally, here is the infamous save image method that is the root of current problems :
#SuppressLint("WrongThread") // not sure what to do otherwise about this lint
public void saveImage() throws IOException {
//create a filename and canvas
String filename = "appName" + System.currentTimeMillis();
Canvas canvas;
// loadedBitmap is the edited one, bitmap is a new one if we did not edit anything
if(loadedBitmap != null){
canvas = new Canvas(loadedBitmap);
} else{
canvas = new Canvas(bitmap);
}
// save image handle for newer api that has scoped storage and updated code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// feed all the data to content values
OutputStream fos;
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename + ".jpg");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
draw(canvas);
// compress correct bitmap, loaded is edited, bitmap is new art
if(loadedBitmap != null){
loadedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
}else{
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
}
Objects.requireNonNull(fos).close();
imageSaved = true;
} else {
// for api older then 28 before scoped storage
// create app directory and a new image file
ContextWrapper cw = new ContextWrapper(getContext());
File directory = cw.getDir("files", Context.MODE_PRIVATE);
pathString = cw.getDir("files", Context.MODE_PRIVATE).toString();
File myPath = new File(directory, filename + ".jpg");
FileOutputStream fileOutputStream = new FileOutputStream(myPath);
// check permissions
try {
if(ContextCompat.checkSelfPermission(
context, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED){
draw(canvas);
// save edited or new bitmap to private internal storage
if(loadedBitmap != null){
MediaStore.Images.Media.insertImage(context.getContentResolver(), loadedBitmap, filename, "made with appName");
}else{
MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, filename, "made with appName");
}
// now also add a copy to shared storage so it will show up in phone's gallery
addImageToGallery(myPath.getPath(), context);
imageSaved = true;
}else{
// request permissions if not available
requestPermissions((Activity) context,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
100);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Save image to gallery method used above :
// for old api <28, it adds the image to shared storage and phone's gallery
public static void addImageToGallery(final String filePath, final Context context) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
That is all, if anyone knows bitmap, canvas and permissions (old and new) feel free to help. I am sure tha changes I need are small. Check the current issues above in order to know what to look for.

Camera API android 10

Error showing while capturing image ,I already give the read write storage permission of device
I am using camera intent for taking photo in android 10, But shows following error
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentResolver resolver = getContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "temp");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + "TEMP");
Uri imageUri123 = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
imageUri123);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}else {
File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.M?getOutputMediaFileUri(getActivity(), photo):Uri.fromFile(photo));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}

Converting Bitmap into a valid Uri

I am developing an android app where user is selecting the image either from gallery or capture from camera. When user get the image from gallery i get the image uri then i pass this uri to other activity in string form. then in next activity i convert that string into uri and then uri into bitmap and set the image bitmap in imageview. Now when i capture the image from camera i get the image bitmap.
Now i want to convert this bitmap into valid uri and pass to next activity
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
System.out.println("URLLL "+selectedImage);
Log.v("PhotoActivity", "Captured image");
//Create intent
Intent intent = new Intent(MainActivity.this, FlagDisplayActivity.class);
intent.putExtra("URI", selectedImage.toString());
//Start Flag Display activity
startActivity(intent);
Log.v("PHOTO ACTIVITY", " uri: " + selectedImage);
}
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(MainActivity.this, FlagDisplayActivity.class);
intent.putExtra("URI", photo);
//Start Flag Display activity
startActivity(intent);
}
This is how i get the uri in next activity
String imageUriString=getIntent().getStringExtra("URI");
final Uri selectedImage=Uri.parse(imageUriString);
and then convert the uri into bitmap like this
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
selectedImage);
} catch (IOException e) {
e.printStackTrace();
bitmap=StringToBitMap(imageUriString);
}
My main goal is to convert the bitmap into uri
You write your Bitmap into the local Cache of the Application and retrieve it from there.
Bitmap photo = (Bitmap) data.getExtras().get("data");// Get the Bitmap
val file = File(context.cacheDir,"CUSTOM NAME") //Get Access to a local file.
file.delete() // Delete the File, just in Case, that there was still another File
file.createNewFile()
val fileOutputStream = file.outputStream()
val byteArrayOutputStream = ByteArrayOutputStream()
photo.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream)
val bytearray = byteArrayOutputStream.toByteArray()
fileOutputStream.write(bytearray)
fileOutputStream.flush()
fileOutputStream.close()
byteArrayOutputStream.close()
val URI = file.toURI()
Now you can send the URI to another Activity as a String and retrieve the URI from the String and get the Bitmap from the URI.
Intent intent = new Intent(MainActivity.this, FlagDisplayActivity.class);
intent.putExtra("URI", URI.toString());
//Start Flag Display activity
startActivity(intent);
Provide the path of image it will provide you image uri
Uri selectedImageURI = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageURI));
Uri yourUri = Uri.fromFile(f);
Use the following function so you will get image
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
Try this:
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);

get image uri from Intent(MediaStore.ACTION_IMAGE_CAPTURE)

Hi when I do something like this
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
In onActivityResult I get thumbnail of the image from the data intent from the camera app
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
And I use that bitmap something like this.
But What if I want the imageuri so that I can get the full size image from it. I tried getting the image uri from the intent something like above
Uri uri = data.getData();
if (uri != null) {
Log.d(TAG, uri.toString());
}else{
Log.d(TAG,"uri is null");
}
Doing like this I get uri is null in my logcat.So can anyone let me know how to get the image uri.I dont want to use EXTRA_OUTPUT and specify my own path.Thanks in advance
There is a well documented bug, which occurs in low resolution devices. Check this thread for the workaround.
There's a bug with that intent in some devices. Take a look at this to know how to workaround it.
In some devices, the Uri is null in onActivityForResult(). So you need
to set Uri to placing the captured image.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// If there any applications that can handle this intent then call the intent.
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Uri fileUri = Uri.fromFile(getOutputMediaFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, CAMERA_PICKER);
}
public File getOutputMediaFile() {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir;
// If the external directory is writable then then return the External pictures directory.
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
} else {
mediaStorageDir = Environment.getDownloadCacheDirectory();
}
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}

Categories

Resources