Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

image - GetExternalStoragePublicDirectory Android 10+ upgrade issue: trying to get the Bitmap created from external app (OfficeLens) into my app

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Uri uri = Uri.parse("content://media/external/images/media/1563");

InputStream is = getContentResolver().openInputStream(uri);

Bitmap bitmap = BitmapFactory.decodeStream(is);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...