Ali*_*Ali 4 android android-imageview
不确定我在哪里犯了错误,但是ImageView一旦用户在图像选择器中选择了图像,我已经尝试了所有方法来显示图像,但是让我们将图像选择器放在一边我什至无法通过直接提供 FullPath 来显示图像如下面的代码(也没有出现任何错误)
尝试 1 - 位图
string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
var imgFile = new File(IMGpath);
if (imgFile.Exists())
{
Bitmap bitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath);
_imageview.SetImageBitmap(bitmap);
}
else
{
Log.Info("AAABL", "No file");
}
Run Code Online (Sandbox Code Playgroud)
尝试 2 - 带选项的位图
string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
var imgFile = new File(IMGpath);
if (imgFile.Exists())
{
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath, bmOptions);
bitmap = Bitmap.CreateScaledBitmap(bitmap, 200,200, true);
_imageview.SetImageBitmap(bitmap);
}
else
{
Log.Info("AAABL", "No file");
}
Run Code Online (Sandbox Code Playgroud)
尝试3 - 自定义库 另外,我尝试使用一个库 如下
string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
var imgFile = new File(IMGpath);
if (imgFile.Exists())
{
var a = ImageService.Instance.LoadFile(imgFile.AbsolutePath)
.Retry(3, 200)
.Into(_imageview);
}
else
{
Log.Info("AAABL", "No file");
}
Run Code Online (Sandbox Code Playgroud)
尝试 4 - URI
string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
Android.Net.Uri URI = Android.Net.Uri.Parse(IMGpath );
_imageview.SetImageURI(URI );
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都无法加载图像。我在清单中有以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
并且还检查活动OnCreate方法的权限以确保应用程序具有以下存储的完全权限
if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted) && (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted))
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
}
Run Code Online (Sandbox Code Playgroud)
这只发生在 Android 10 (API 29) 中,其中相同的代码在 API 28 之前都可以正常工作。
这是一个灵魂粉碎者,一整天我都想不通,知道吗?
更新:: 尝试打开文件时出现以下错误
打开失败: EACCES(权限被拒绝)
小智 5
我遇到了同样的问题并找到了您的帖子,然后在这里为您找到了一个潜在的解决方案:https : //github.com/bumptech/glide/issues/3896#issuecomment-533062076
只需将此添加到清单的应用程序标签即可。它在 10 上为我解决了这个问题:
android:requestLegacyExternalStorage="true"
| 归档时间: |
|
| 查看次数: |
4324 次 |
| 最近记录: |