繁体   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