【问题标题】:Saving file shows Permission Denied though permission granted [duplicate]保存文件显示 Permission Denied 虽然已授予权限 [重复]
【发布时间】:2018-10-18 05:17:43
【问题描述】:

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

 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/emulated/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.- 我有 2 个真实设备。它在 Mashmallow 和 Nougat 模拟器中工作,但问题仅在奥利奥中。

【问题讨论】:

  • 代码sn-p中没有地方,负责权限检查的代码。请添加所有相关代码。没有它,我们只能猜测这里出了什么问题。
  • 您能否解释一下您获得了哪些权限?
  • @AnuChaudhary 读写。
  • @Jay 编辑了问题。
  • Unable to save image file in android oreo update. How to do it? 的可能重复项(根据下面的评论)

标签: android android-permissions


【解决方案1】:

您可以尝试以下方法:

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();

请参考help

【讨论】:

  • 不要只是从任何地方复制粘贴。首先,理解问题,然后发布答案。
  • 我已经检查了您的问题,然后发现它合适。所以发给你了。
  • 以上你试过了吗?
  • 如果你检查过,那么你一定知道我的代码是你的代码的更新版本。
  • 感谢您的帮助,但我得到了解决方案。 stackoverflow.com/questions/47787577/…
【解决方案2】:

您应该在 Android Mainifest 中添加 WRITE_EXTERNAL_PERMISSION,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2018-10-17
    • 2013-12-12
    • 2013-06-07
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多