I'm pretty new to the Android world, and I'm facing problems with an app written in Xamarin Android Mono environment.
I need to upgrade Android Target API at least to 29 (better 30), and I stuck at "scoped storage" change.
Although I was able to manage my own pictures taken by the camera (introducing the FileProvider), I still have problem with external images taken by the app Microsoft Office Lens (stored in public folder Pictures/Office Lens).
Summarizing, I use to call OfficeLens by an intent from my app:
StartActivity(Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage("com.microsoft.office.officelens"))
checking for new files with the "deprecated" GetExternalStoragePublicDirectory, that is still working for my porpouse (such as read the list of Office Lens file in order to find new ones):
var TmpDir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "Office Lens");
Read the NewFiles saving the paths into a list:
var LstFilesNew = new List<string>();
...
foreach (var I in TmpDir.ListFiles())
{
if (I.IsFile == true)
{
LstFilesNew.Add(I.Path);
}
}
Whether before simply used to call this function passing the FilePath to get the BitMap:
public static Bitmap LoadBitmap(this string fileName)
{
var options = new BitmapFactory.Options();
options.InJustDecodeBounds = false;
return BitmapFactory.DecodeFile(fileName, options);
}
With scoped storage I'm trying to get the URI, from the URI to get the Bitmap via MediaStore:
foreach (var FileNew in LstFilesNew)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
{
ContentValues contentValues = new ContentValues();
contentValues.Put(MediaStore.MediaColumns.DisplayName, FileNew);
contentValues.Put(MediaStore.MediaColumns.MimeType, "image/jpg");
contentValues.Put(MediaStore.MediaColumns.RelativePath, Environment.DirectoryPictures);
Uri imageUri = Application.Context.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, contentValues);
fos = Application.Context.ContentResolver.OpenOutputStream(imageUri);
Android.Graphics.Bitmap finalBitmap = MediaStore.Images.Media.GetBitmap(Application.Context.ContentResolver, imageUri);
finalBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, fos);
fos.Close();
}
}
My URI seems to be ok ("content://media/external/images/media/1563") but I obtain a "finalBitmap" null after calling "MediaStore.Images.Media.GetBitmap".
I guess I'm completely wrong in my approach, but I spent some days to trying and googling without any result.
Any helps about how to get a bitmap from pictures taken by a third party application in Pictures/"xxx" it'll be really appreciated.
Thank you in advance, cheers
question from:
https://stackoverflow.com/questions/65938014/getexternalstoragepublicdirectory-android-10-upgrade-issue-trying-to-get-the-b