簡體   English   中英

GetExternalStoragePublicDirectory Android 10+ 升級問題:試圖將 Bitmap 從外部應用程序(OfficeLens)創建到我的應用程序中

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

我對 Android 世界還很陌生,我遇到了用 Xamarin Android Z5D90Z657BD3B6501DA8D5 環境編寫的應用程序的問題

我需要將 Android 目標 API 至少升級到 29(最好是 30),並且我堅持“范圍存儲”更改。 雖然我能夠管理我自己的相機拍攝的照片(引入 FileProvider),但我仍然對應用 Microsoft Office Lens 拍攝的外部圖像(存儲在公共文件夾 Pictures/Office Lens 中)有問題。

總而言之,我曾經通過我的應用程序的意圖調用 OfficeLens:

StartActivity(Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage("com.microsoft.office.officelens"))

使用“已棄用”的 GetExternalStoragePublicDirectory 檢查新文件,該文件仍然適用於我的 porpouse(例如閱讀 Office Lens 文件列表以查找新文件):

var TmpDir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "Office Lens");

閱讀將路徑保存到列表中的 NewFiles:

var LstFilesNew = new List<string>();
... 
foreach (var I in TmpDir.ListFiles())
{
  if (I.IsFile == true)
  {
    LstFilesNew.Add(I.Path);
  }
 }

之前是否簡單地調用這個 function 通過 FilePath 得到 BitMap:

public static Bitmap LoadBitmap(this string fileName)
{
  var options = new BitmapFactory.Options();

  options.InJustDecodeBounds = false;

  return BitmapFactory.DecodeFile(fileName, options);
}

使用范圍存儲,我試圖獲取 URI,從 URI 到通過 MediaStore 獲取 Bitmap:

    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();
        }
    }
  

我的 URI 似乎沒問題(“content://media/external/images/media/1563”),但我在調用“MediaStore.Images.Media.GetBitmap”后獲得了“finalBitmap”null。

我想我的方法完全錯誤,但我花了幾天時間嘗試和谷歌搜索,但沒有任何結果。

關於如何從圖片/“xxx”中的第三方應用程序拍攝的圖片中獲取 bitmap 的任何幫助,我們將不勝感激。

提前謝謝你,干杯

Uri uri = Uri.parse("content://media/external/images/media/1563");

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

Bitmap bitmap = BitmapFactory.decodeStream(is);

GetExternalStoragePublicDirectory 仍在針對 Android 10 和 Android 11 檢索圖像/xyz 的路徑。

此外,仍然可以像這樣獲得 bitmap:

public static Bitmap LoadBitmap(this string filePath)
{
  var options = new BitmapFactory.Options();

  options.InJustDecodeBounds = false;

  return BitmapFactory.DecodeFile(fileName, options);
}

其中 filePath 是使用 ListFiles() 方法從 GetExternalStoragePublicDirectory 的結果中讀取的:

var TmpDir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "xyz");

foreach (var I in TmpDir.ListFiles())
{
  if (I.IsFile == true)
  {
    var Bmp = LoadBitmap(I.Path);
  }
} 

感謝@blackapps 的點擊!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM