gpt4 book ai didi

Android 6.0+ java.io.FileNotFoundException : (Permission denied)

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:10 29 4
gpt4 key购买 nike

最近,我遇到了一个关于权限问题的非常奇怪的问题,让我困惑了几天,我希望有人能帮我弄清楚这是怎么回事。谢谢!

问题来了:出于某种原因,我需要申请存储权限来保存一些照片,我已经将权限添加到 AndroidManifestival.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

当然,我知道在Android 6.0之后的代码中我必须申请存储权限,这里是我申请权限的代码:

public static boolean isGrantExternalRW(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {

((Activity)context).requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, Constants.PERMISSION_REQUEST_CODE);

return false;
}

return true;
}

我在保存照片之前使用过这种方法,并且出现了权限对话框并且我已经检查过了。而且我已经在应用设置中检查了应用权限,没问题。

这是我用来将位图保存到文件的方法:

public static String savePhotoToSDCard(Bitmap bitmap, String photoName) {
String absolutePath = "";

String state = Environment.getExternalStorageState();
Log.e("file", "---------------------" + state);
File newsDir = new File(Environment.getExternalStorageDirectory().toString()
+ File.separator + "photo");


if (!newsDir.exists()) {
newsDir.mkdirs();
}

File picFile = new File(newsDir, photoName + ".png");
absolutePath = picFile.getAbsolutePath();

if (picFile.exists()) {
picFile.delete();
}

if (photoName.equals("avatar")) {
int ratio = 2;
Bitmap result = Bitmap.createBitmap(bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio);
canvas.drawBitmap(bitmap, null, rect, null);
}

try {
FileOutputStream fileOutputStream = new FileOutputStream(picFile);
if (bitmap != null) {

if (bitmap.compress(Bitmap.CompressFormat.PNG, 30, fileOutputStream)) {
fileOutputStream.flush();
Log.d("Photo", "---------------------------fileOutputStream.flush");
fileOutputStream.close();
}
}
} catch (FileNotFoundException e) {
Log.e("File", "----------------------------" + e.toString());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return absolutePath;
}

当我运行应用程序时,它出错了,这是日志:

java.io.FileNotFoundException: /storage/emulated/photo/avatar.png(permission denied)

但最奇怪的是,当我在AndroidManifestival.xml中两次添加存储权限时,它起作用了!

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

那么,问题是什么?请帮忙。

最佳答案

幸运的是,我终于解决了我的问题。这是原因:存储权限与以下依赖冲突:

compile 'com.github.vungle:vungle-android-sdk:5.3.0'

当我添加依赖时,系统无法识别我申请的存储权限(虽然我在 AndroidManifest.xml 中添加了两次,但它有效),但是当我删除它时,一切正常!这个依赖是用来在应用中添加广告的,但现在我不得不放弃它。

关于Android 6.0+ java.io.FileNotFoundException : (Permission denied),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198045/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com