Android get the actual filepath from gallery or google photos - android

I am trying to get the actual path of an image so that I can upload or send to server as multipart/form-data. So, Once I get the Uri, I have below code
Uri selectedImageUri = data.getData();
File file = new File(selectedImageUri.getPath());//create path from uri
final String[] split = file.getPath().split(":");//split the path.
String path = split[1];//assign it to a string(your choice).
uri value = content://com.android.providers.media.documents/document/image%3A32
path value = 32
I don't get it why path is a number, even I tried using getcontentResolver() and with that too I am getting path =32. I am using API 29 so I can't use MediaStore.Images.Media.DATA so, I have to use MediaStore.Images.Media._ID. It would be good if someone can also tell how to upload image as form-data too once i have path.

Related

creating file from uri in android 11 - getPath() not working

I want to create a file from uri in the ActivityResultCallback in android 11. I use uri.getPath() to convert the incoming uri to a file. but, it won't work in android 11. here is my codes:
private void launchGallery (){
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
launcherGallery.launch(intent);
}
ActivityResultLauncher<Intent> launcherGallery = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
if (data != null) {
Uri imageUri = data.getData();
// ---> getPath() won't work in android 11 <---
File file = new File(imageUri.getPath());
// I don't want to display the image in an ImageView.
// I need a file object to pass it to this method to encrypt it
encryptImage(file);
}
}
});
so, how can I create a file from uri in android 11?
simply: you can't. if Uri would be always a File then there would be no purpose of both class existance. Uri is a "pointer" to a file, which can be read as InputStream
InputStream inputStream = getContentResolver().openInputStream(uri);
you have to read all these bytes and if you REALLY need a File then store this data in your apps Scoped Storage. if you are shure that this is an image (its declared in calling Intent) then you may try to convert this data to Bitmap.
edit: I see you've added encryptImage(..) method call, so for shure you can read Bitmap "straight from" Uri instead of a File without taking users storage space, even for a while
check out such case: note that when you run a file picker launchGallery() then inside of it you can switch folders. there is a possibility to pick file from e.g. Google Drive, or if you have installed, other web-storages (OneDrive, Dropbox etc.). you will get an Uri as a result, but this isn't a real File on local storage. so you can stream it (as through web/network) for reading entirelly, it isn't ready and available at the moment of result callback call (just after picking and returning to your app)
btw. such possibility isn't limited to Android 11 and above, it was there way earlier. just handle Uri as "pointer", do not assume it is pointing on (local) File. in some cases even if user pick local file you will get an Uri pointing on it through some ContentResolver (so use own for read data), thus this won't be a file path (won't be starting with file://)

Load image from gallery doesn't works twice

I select an image from the gallery. Now I load it in a temp-file and can show it in a ImageView. Everything works fine.
Uri uri = data.getData();
File file = new File(uri+"");
String fileName = file.getName();
String filePath = file.getParent();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
//here come's the code to save the temp-file and so on
In the fileName and filePath I save the name and path for later use. Because if the user want to save the image I want to make a copy of them.
URI shown in the debugger: content:/media/external/images/media/12345
filePath: content:/media/external/images/media
fileName: 12345
If I want to save the image I use the fileName and filePath to build the URI and want to do the same like above. But it doesn't works. I get a FileNotFoundException "no content provider" from the MediaStore. Hmm - before I deleted the App for a fresh testing there comes "No such file or directory" and I thought I eliminate the "no content provider" Exception with the READ_EXTERNAL_STORAGE permission.
Uri uri = Uri.parse(filePath +"/"+ fileName);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
Here I see also the correct URI in the debugger.
What is wrong?
I've set the permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in the AndroidManifest.
In the fileName and filePath I save the name and path for later use
A Uri is not a file, and yours most certainly is not a file.
What is wrong?
Your Uri has a content scheme. This is being served by a ContentProvider. You do not indicate where this Uri comes from, but I am going to guess that it is from ACTION_GET_DOCUMENT or something similar. In that case, like a Web URL that requires a working session, your access to the content identified by that Uri is short.
If you want to have longer-term access to the content, download it into your app, just as you would download content from a Web URL.

ExifInterface_JNI: Raw image not detected error

When trying to get the ExifInterface I keep seeing a Raw image not detected error message.
ExifInterface exifInterface = new ExifInterface(filepath);
int rotation=exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);
Does anyone know what could be causing this?
I am getting it from a Uri but I know the filepath exists
Those statements are mutually contradictory. A Uri is not a file. If the scheme of the Uri is file, then and only then can you get a filesystem path to the file, by means of getPath(). If the scheme is anything else, such as content, then you cannot get a filesystem path, because there is no requirement that there be a file. For example, a Uri of http://stackoverflow.com/questions/42930509/exifinterface-jni-raw-image-not-detected-error does not mean that the Android device has a file at /questions/42930509/exifinterface-jni-raw-image-not-detected-error.
The ExifInterface from com.android.support:exifinterface (e.g., where the current latest version is 25.3.0) has a constructor that takes an InputStream. Create a ContentResolver (via getContentResolver() on a Context, such as your Activity). Call openInputStream() on that ContentResolver, supplying the Uri (works for both file and content schemes). Pass that InputStream to the library's ExifInterface constructor. This simultaneously ensures that you do not cause security problems for your users and avoids having to worry about getting a filesystem path for the content that you wish to examine.
Long story short, apply the below example to your code:
Uri uri = Uri.fromFile(f);
// where f of type File
in = context.getApplicationContext().getContentResolver().openInputStream(uri);
// context should refer to your context app
ExifInterface exifInterface = new ExifInterface(in);
// you'll need "exifInterface"
And don't forget to close your input stream
in.close();

Android how to get path of mediastore?

I'm trying to get the path were android save the images. With this code:
String path = MediaStore.Images.Media.insertImage(getContentResolver(), myImage, "Title","Description").toString();
I get the path of the image that /mnt/sdcard/DCIM/Camera/1405408110218.jpg but I want only the path of the folder like /mnt/sdcard/DCIM/Camera. Please don't tell me to use split and join because it's not what I want
Use this:
Uri path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Or this:
Uri path = MediaStore.Images.Media.INTERNAL_CONTENT_URI;

Real path of Android pictures from photo gallery

I would like to do image processing on pictures from the photo gallery.
So, I'm working on module that works on android Bitmap by reading an image file.
From my Titanium application, I open the photo gallery and send the event.media.file.nativePath to my module.
But, it seems that it can't find it because when I create a
File file = new File(path)
I get a file.exists() == false. The path I get from the media looks like this on my phone :
file:///content://........../data/data.........jpg
Is there a way to create an Android Bitmap from media of Ti.Media.openPhotoGallery ?
"file:///content://........../data/data.........jpg"
This file path seems to be like on BlackBerry. On you can get external storage like this:
Environment.getExternalStorageDirectory().getAbsolutePath();
Then you can add path to your file.
Please! try below sample code.
Uri selectedImageUri = **put your content url**;
String[] filePathColumn = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(
, filePathColumn, null, null, null);
.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);

Categories

Resources