Android how to get path of mediastore? - android

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;

Related

Android get the actual filepath from gallery or google photos

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.

Get content:// Uri from File

I have a File object and I want to get its Uri, that looks like:
content://media/external/images/media/2683
I've tried the following code:
val uri = FileProvider.getUriForFile(activity, "${BuildConfig.APPLICATION_ID}.file_provider", pictureFile)
But I'm getting this:
content://my.package.name.file_provider/external_files/Pictures/photo-1574403815163.jpg
Is it possible to get the Uri I need?
UPD: The reason I want to get another Uri - I can't create File object from this Uri later.
val uri = FileProvider.getUriForFile(activity, "${BuildConfig.APPLICATION_ID}.file_provider", pictureFile)
val file = File(uri.path)
When I try to file.exists() I'm always getting false.
I can't create File object with this Uri
That's because you don't do that. Moreover, you don't need that. pictureFile is a File.
If your are creating file with actual path then you just concad the content:// to that path
Iam not sure this is correct method or not but i get that what you expected , Try this one

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.

Get fileName from uri on KitKat Document

I want to get fileName from a uri... I can get a fullpath from Other apps like gallery or other third party file managers, but I can't get fileName from KitKat Document, I don't need a fullPath just the fileName with an inputStream is good for me...
Thanks
Edit: I found a piece of code that solves the issue almost, but it can't get filePath of none known types like *.exe *.vcf , ...
Here's the source
If you have the absolute path of file then simpley create the object of file and use getName method
File f = new File(<Full Path of your file>);
This will return file name with extension.
ANd if you do not want extension
then use CommonsI/O libarary
And from that library you can use
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);

How to get Uri of res folder?

I am trying to get the Uri of an image I have in the drawable folder. I tried many possible ways but nothing seems to work. Can anyone suggest me how to get the Uri of res folder. Any help is much appreciated.
Well, it's actually quite easy. a base URI for a resource in your package would be something like the following possibilities:
android.resource://[package]/[resource_id]
android.resource://[package]/[res type]/[res name]
So the following would be managable.
String pkgName = getApplicationContext().getPackageName();
Uri path = Uri.parse("android.resource://"+pkgName+"/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://"+pkgName+"/drawable/icon");
You can find more about this at http://androidbook.blogspot.com/2009/08/referring-to-android-resources-using.html

Categories

Resources