Android Upload Image using path or uri? - android

I'm having a trouble bringing the images from the gallery to my app.
The hard part is that, in some articles they use uri rather than path, but in others vice versa.
Plus, I'm not also sure... when I get Uri from the intent coming back, should I use cursor to get images data? (How to get Images from Cursor in android?) In some other references, they do it in the easy way, just with 'getPath()' method.
Do I need either of path or uri? or Only one of these?
I'm so confused now..

Always use Uri:
File file = new File(uri.getPath());
then
Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());
and when you have bitmap simply load it in to ImageView
imageView.setImageBitmap(bitmap);

Related

Scoped storage - is it still possible to get image Uri by absolute path, without SAF?

I understand that from api 29 we need to work with URI and Image id's to perform bitmap operations, but in order to migrate from image path to uri i'll need to drop real users databases and server databases, and if there is a way to avoid this i would like to try.
For example i got "FileNotFoundExeption: no content provider" error here:
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(path)), new Rect(), options);
Thanks!
edits:
path is absolute file path like /storage/emulated/0/DCIM/Camera/20200128_122949_020.jpg
Here in code snippet i'm trying to decode image bounds, e.g. to get width and height of image on filesystem by path of image.

Rotated images on Android

When I take camera images from a gallery, the images are rotated. I've read thousands of tutorials about this and how to fix it, but I'm still confused (I'm a bad programmer and beginner). I understand that to solve the problem I need to use an Exif class, but...
First, in onActivityResult, when I get URL selectedImage = data.getData(), how do I get the Path of this? Should I use Path or Name of image, and how do I get the path or name? Now, I understand Exif works with files, not bitmaps? And I must make File, and file search the path of an image: so, after I get the URL selectedImage, how do I make that file use exif? How can I get the path of the URI?
Excuse my enormous ignorance; if anyone can help me I'll be grateful!

Take photo of given size Android

I'm developing an application that takes a photo and saves it in Android/data/package/files. I would like to reduce storage used, so I would like to resize the photo before saving it. For the moment I'm calling new intent passing as extra the output path. Is possible to pass also the size wanted or is possible to have the bitmap before saving it?
public void takePicture(View view){
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(getPath(nameEdit.getText().toString()));
path = f.getAbsolutePath();
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
if (pictureIntent.resolveActivity(getPackageManager())!=null){
startActivityForResult(pictureIntent,REQUEST_IMAGE_CAPTURE);
}
}
Is possible to pass also the size wanted
No.
is possible to have the bitmap before saving it?
Not really. You could write your own ContentProvider and use a Uri for that, rather than a file. When the camera app tries saving the content, you would get that information in memory first. However, you will then crash with an OutOfMemoryError, as you will not have enough heap space to hold a full-resolution photo, in all likelihood.
You can use BitmapFactory with inSampleSize set in the BitmapFactory.Options to read in the file once it has been written by the camera, save the resized image as you see fit, then delete the full-resolution image. Or, skip EXTRA_OUTPUT, and you will get a thumbnail image returned to you, obtained by calling getExtra("data") on the Intent passed into onHandleIntent().

what is the best way to work with Images in SqLite android?

I am working with a customizable database with pictures. Right now I am taking pictures as it is from the sdcard and encoding it in base64 String and then putting it in the database. but whenever I am trying decoding it and showing it in my view, I am getting Out of memory error. Can any one one tell me what is the best procedure to do it? Shall I change the size of the pictures before encoding it?
I want to re-size all of the pictures into 512*512.
Image to Base64 is very heavy operation in android. Consider saving the images on the external/internal memory and save the file path in the sqlite database.
You can convert your image to byte array then store values in sql by using BLOB type and vice versa.
As you mentioned you want to resize the images to 512*512, you can scale the image using below code,
Create bitmap from captured image and then use below line
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 512, 512, false);
It will give you a smaller image, you can also consider compressing the image to reduce in size,
OutputStream imagefile = new FileOutputStream("/your/file/name.jpg");
// Write 'bitmap' to file using JPEG and 50% quality hint for JPEG:
bitmap.compress(CompressFormat.JPEG, 50, imagefile);
Now, you have two options,
Save the scaled and compressed image into a file and save the path of that file in db. (Better way)
Convert the scaled and compressed image to base64 string and save in db.
Althought base64 is , as many answers said, a heavy operation for android, if done properly, it should not be a problem.
There are many reasons a bitmap could be required to be saved to a DB , (photo of a invoice ticket, for example?), and this is the way i do it.
first, create a new , smaller bitmap like #Swapnil commented.
and second, correctly use the bitmap transformation methods, i've been using these (look below) two so far and haven't had any memory issue on many different devices.
link to my BitmapUtils transformation methods

android send email with an image screenshot created but not saved

I want to make a screenshot of the screen without saving the image, now I do this to make a screenshot:
View view = webView.getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
result = new PluginResult(PluginResult.Status.OK);
And to attach the image to the email:
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///INFINITA-PL.png"));
I do not know how to do it without path.
The EXTRA_STREAM value has to be a Uri that can be opened by the Email process. If you don't want to save it as a file and pass that along you will need to implement a ContentProvider to make it accessible. In general this is pretty messy to do: I can echo the comments in that question (I struggled for awhile to do it without hitting the file system before giving up).
You're probably better served using a File and moving on to the rest of your app.

Categories

Resources