GetRealPath for image Android10 - android

I am trying to get real path for Picked image in Android 10 but it's not working Iam using the folowing code its work fine in android 9
public static String getRealPathFromURI(Context context, Android.Net.Uri contentUri)
{
ICursor cursor = context.ContentResolver.Query(contentUri, null, null, null, null);
cursor.MoveToFirst();
String document_id = cursor.GetString(0);
document_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
cursor.Close();
cursor = context.ContentResolver.Query(
Android.Provider.MediaStore.Images.Media.ExternalContentUri,
null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new string[] { document_id }, null);
cursor.MoveToFirst();
String path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
cursor.Close();
return path;
}

Option1
After SDK 29.0 ,DATA is deprecated . Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access.
Here is a similar thread which you can refer .
Option 2
You could install the plugin Xam.Plugin.Media from nuget .
Usage
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
var path = file.Path;
For more details about the plugin you could check https://github.com/jamesmontemagno/MediaPlugin

Related

How to get the filename of a shared WhatsApp text file in Xamarin Forms Android?

I used to use the following method to get the name of a text file shared via WhatsApp to my app:
string GetIntentFilename(Android.Net.Uri uri)
{
string filename;
string temp;
using (var c1 = ContentResolver.Query(uri, null, null, null, null))
{
c1.MoveToFirst();
temp = c1.GetString(0);
filename = temp.Substring(temp.LastIndexOf(":") + 1);
}
return filename.Trim();
}
If I shared a file 123.txt, the above method would return "123.txt"
Recently however it has been returning me a weird name, similar to the following: "DOC-20230109-WA0008."
I've tried sharing the file with other apps and they successfully extract the 123.txt filename, so I'm sure the information exists somewhere in the intent, but I don't know how to get it.
Any ideas?
Refer to this answer about Android Studio get shared file, name of Uri, from whatsapp to my app, you can try the following code:
string GetIntentFilename(Android.Net.Uri uri)
{
string filename;
using (var c1 = ContentResolver.Query(uri, null, null, null, null))
{
// var index = c1.GetColumnIndex(OpenableColumns.DisplayName);
// the OpenableColumns.DisplayName has been deprecated, you can try the next line code
var index = c1.GetColumnIndex(IOpenableColumns.DisplayName);
c1.MoveToFirst();
filename = c1.GetString(index);
}
return filename;
}

How to get path of media files in android

I want paths of files shared through share intent from any app. I do get path of files like zip, apk, pdf, etc. with code ShareCompat.IntentReader.from(this).getStream().getPath();.
But but I don't get path for image, video and audio files I get content://media/external/video/media/40666, How to get real path of files of this type?
If all you want is the path, you can use the following code
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+1);
cursor.close();
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null
, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
It basically uses he ContentResolver to get the data of the file.
You can change the query parameters to get what you want.
Or you can use
getContentResolver().openInputStream(uri)
If you want the entire file

Getting Album cover on Android 11 (R)

After updating on Android R, my app can't loading albums' art.
Code below working on Android Q
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumId);
Size size = new Size(700, 700);
return contentResolver.loadThumbnail(uri, size, null);
And also code below working on Android P and earlier
private Bitmap getAlbumArt(int albumId) {
try (Cursor cursor = contentResolver.query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.AlbumColumns.ALBUM_ART},
_ID + "=?",
new String[]{String.valueOf(albumId)},
null)) {
if (cursor == null || !cursor.moveToFirst()) {
return null;
}
String artLink = cursor.getString(0);
return BitmapFactory.decodeFile(artLink);
}
}
The problem is that there is no information in documentation about extracting album art.
After updates file storage access policy this is not working.
Seems it was deprecated. Look there

Android: Get file path from Uri of file in SD card

We are trying to fetch the path of a file that is saved in SD Card, to pass on to a utility. However when we use the URI to open a cursor, only two columns are being fetched, the path (_data column) is missing. Please let me know how to fetch the path. The query works fine for any file that is in internal storage.
Scheme is "content".
Code snippet
cursor = context.getContentResolver().query(uri, null, null, null, null);
if (null != cursor && cursor.moveToFirst())
{
int testcolCount = cursor.getColumnCount();
for (int i = 0; i < testcolCount; i++)
{
String colName = cursor.getColumnName(i);
String colValue = cursor.getString(i);
System.out.println("" + i + ": " + colName + ":" + colValue + "\n");
}
//This prints only two columns, _display_name and _size
}
Don't try and get the path at all, with Android 10 and above file paths outside of your App's private directories are useless because you won't have permission to access them.
See https://developer.android.com/training/data-storage#scoped-storage
Use the contentResolver to get a FileDescriptor to pass on to the Utility
Note From https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#DATA
This constant was deprecated in API level 29.
Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access.
try this code i am using this function to get real path from uri:
private String getRealPathFromURI(Context mContext, Uri contentURI) {
String result = null;
Cursor cursor = null;
try {
String[] proj = {MediaStore.Video.Media.DATA};
ContentResolver mContentResolver = mContext.getContentResolver();
String mime = mContentResolver.getType(contentURI);
cursor = mContentResolver.query(contentURI, proj, null, null, null);
if (cursor == null) {
return null;
} else {
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
if (column_index > -1)
result = cursor.getString(column_index);
cursor.close();
}
} catch (Exception e) {
return null;
} finally {
if (cursor != null)
cursor.close();
}
return result;
}
it will return a string which is real path of your file.
hope it will help you.
The default File manager was the one causing this problem - not returning "DATA" column and returning Authority as "com.lenovo.FileBrowser.FileProvider". When I used another file manager, the Authority was "media". Also got the path column using "DATA" while running the ContenResolver. Thanks to all for taking the effort to help out.

Unable to fetch file path from uri in Android M

I am able to get the files path from URI fetched from notification bar but I could only fetch it from URI received from Android Snackbar.
Current function used to fetch the path:
Note: This works fine for pre-Marshmallow Android versions.
private String extractContentUri(Uri uri) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, null, null, null, null);
Log.d("CHECK", "COUNT" + cursor.getCount());
if (cursor != null && cursor.moveToFirst()) {
Log.d("CHECK", "CURSOR !NULL");
launchedFile = cursor.getString(cursor.getColumnIndexOrThrow("_data"));
Log.d("CHECK", "==" + launchedFile);
return launchedFile;
}
} catch (IllegalArgumentException e) {
} finally {
Log.d("CHECK", "{finally} ==" + launchedFile);
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return null;
}
In android M the URI obtained while accessing the from notification bar and from snackbar are different.
URI from notification bar: content://downloads/all_downloads/1449
URI from snackbar: content://downloads/my_downloads/1449
cursor = getContentResolver().query(uri, null, null, null, null);
here the cursor.getCount() is always 0 for snackbar.
You need to add Runtime permission for Android 6 and above version. Please check this for Run time permission.

Categories

Resources