Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 保存文件显示权限被拒绝,尽管已授予权限_Android_Android Permissions - Fatal编程技术网

Android 保存文件显示权限被拒绝,尽管已授予权限

Android 保存文件显示权限被拒绝,尽管已授予权限,android,android-permissions,Android,Android Permissions,我正在将图像从glide保存到设备。我在第一次运行应用程序时请求许可,因为我的应用程序就是这样做的 final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DesiJewellery/"); public void saveImage(Bitmap bitmap, String img_title) { fname = img_title; myDi

我正在将图像从glide保存到设备。我在第一次运行应用程序时请求许可,因为我的应用程序就是这样做的

 final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DesiJewellery/");
public void saveImage(Bitmap bitmap, String img_title) {

    fname = img_title;
    myDir.mkdirs();
    File image = new File(myDir, fname);


    FileOutputStream outStream;
    if (image.exists()) {
        image.delete();
    }
    try {
        outStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getActivity(), "Design saved - " + img_title, Toast.LENGTH_SHORT).show();
    } else {
       // Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG).show();
    }
    // this one to show in gallery:

}
它在模拟器中运行良好,但在实际设备中显示

java.io.FileNotFoundException:/storage/simulated/0/DesiJewellery/m_aad14.jpg(权限被拒绝)

权限检查。我在MainActivity上运行它

public void isStoragePermissionGranted() {

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {

        } else {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    } else {

    }
}  

p.S.-我有两台真正的设备。它在Mashmallow和Nougat Emulator中工作,但问题只出现在Oreo中。

您应该在Android Mainifest中添加写入外部权限,如下所示:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
tools:node="replace"/>

您应该在Android Main中添加写入外部权限,如下所示:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
tools:node="replace"/>

您可以尝试以下方法:

AsyncTask fileTask = new AsyncTask() {
    @Override
    protected Object doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
                directory.mkdirs();
            }
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
   finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
   out.close();
} catch (Exception e) {
   e.printStackTrace();
}
return null;
 }
};
fileTask.execute();

请参阅此部分,了解您可以尝试以下操作:

AsyncTask fileTask = new AsyncTask() {
    @Override
    protected Object doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
                directory.mkdirs();
            }
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
   finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
   out.close();
} catch (Exception e) {
   e.printStackTrace();
}
return null;
 }
};
fileTask.execute();


请参阅此处,了解负责权限检查的代码片段中没有任何内容。请添加所有相关代码。没有它,我们只能猜测这里出了什么问题。你能解释一下你获得了什么权限吗?@AnuChaudhary read&write@Jay编辑了这个问题。可能重复的(根据下面的注释)代码片段中没有任何地方,负责权限检查的代码。请添加所有相关代码。没有它,我们只能猜测这里出了什么问题。你能解释一下你获得了什么权限吗?@AnuChaudhary read&write@Jay编辑了这个问题。可能的重复(根据下面的评论)不只是从任何地方复制粘贴。首先,理解问题,然后发布答案。我已经检查了你的问题,然后发现它合适。以上内容已发送给您。您已经尝试过了吗?如果您已选中,则您必须知道我的代码是您代码的更新版本。谢谢您的帮助,但我已找到解决方案。不要只是从任何地方复制粘贴。首先,理解问题,然后发布答案。我已经检查了你的问题,然后发现它合适。以上内容已发送给您。您已经尝试过了吗?如果您已选中,则您必须知道我的代码是您代码的更新版本。谢谢您的帮助,但我已找到解决方案。