从本地存储到ImageView的图像文件-android 10存储权限问题
不确定我在哪里出错,但一旦用户在image Picker中选择了图像,我已尽一切努力让图像显示在从本地存储到ImageView的图像文件-android 10存储权限问题,android,android-imageview,Android,Android Imageview,不确定我在哪里出错,但一旦用户在image Picker中选择了图像,我已尽一切努力让图像显示在ImageView中,但是让我们将图像选取器放在一边吧我甚至无法通过直接提供下面代码中的完整路径来显示图像(也没有得到任何错误) 尝试1-位图 string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg" var imgFile = new File(IMGpath); if (imgFile.Exists())
ImageView
中,但是让我们将图像选取器放在一边吧我甚至无法通过直接提供下面代码中的完整路径来显示图像(也没有得到任何错误)
尝试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");
}
尝试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");
}
尝试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");
}
试试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 );
无论我做什么,我都无法加载图像。我在清单中拥有以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这只发生在Android 10(API 29)中,其中相同的代码在API 28之前运行良好。
这真是一个灵魂破碎机,一整天我都想不出来,知道吗
更新:
当我试图打开该文件时,出现以下错误
打开失败:EACCES(权限被拒绝)
您可以尝试一次,我假设您正在使用API23+并在运行时检查权限,但是您可以查看下面的一个
File imgFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
};
对于API 23+您需要请求读/写权限,即使它们已经在您的清单中
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_INTERNAL_STORAGE
);
}
}
并处理响应,例如:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
我遇到了同样的问题,找到了你的帖子,然后在这里为你找到了一个可能的解决方案: 只需将其添加到清单的应用程序标记中。它在10日为我解决了这个问题: android:requestLegacyExternalStorage=“true”您可以将文件(或文件路径)转换为
ContentProvider
URI:
Uri contentUri = FileProvider.getUriForFile(context, "com.myapp.app.fileprovider", imgFile);
InputStream is = context.getContentResolver().openInputStream(contentUri);
如果清单中没有定义,请使用
部分中定义的提供程序
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_INTERNAL_STORAGE
);
}
}
更“手动”的方法如下(例如MediaStore中的图像):
您使用的API级别是什么?23+?是否确实显示图像视图?也许能见度不可见或消失了。尝试使用glide或毕加索在internet上显示图像。同时添加文件.canRead()。这是安卓Q吗?你是如何获得这条道路的?“文件选择器”永远不会给你这样的路径!您使用了类似于从uri获取真实路径的功能。不要做这样的事情。嗨@blackapps我想你有什么问题,我无法读取文件我收到错误
打开失败:EACCES(权限被拒绝)
嗨,我正在检查活动“OnCreate”的权限,并在被询问时单击“授予”-我用以下内容更新了问题info@Ali你能试试我上面提到的代码吗?完全按照你说的做了,不走运,而且我注意到它在Android 10上不起作用,它在所有其他API上都起作用,包括API 28。这将起作用,但这不是一个好答案。不确定这将工作多长时间。可以工作,但不是永久解决方案,在应用程序设置中手动授予存储权限即可工作。由于某些原因,当用户单击“授予”时,androidQ不会授予存储权限。我会尝试一下,这似乎是androidQ上的存储权限问题,在应用程序设置中手动授予存储权限,并且可以正常工作。出于某种原因,当用户单击“授予”时,androidQ不会授予存储权限,你知道为什么吗
Uri uri = null;
Uri photoUri = MediaStore.Images.Media.getContentUri("external");
String[] projection = {MediaStore.Images.ImageColumns._ID};
Cursor cursor = context.getContentResolver().query(photoUri, projection, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[] { filePath }, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
long photoId = cursor.getLong(columnIndex);
uri = Uri.parse(photoUri.toString() + "/" + photoId);
}
cursor.close();
}
InputStream is = context.getContentResolver().openInputStream(uri);