java.io.FileNotFoundException保存图像时拒绝权限

时间:2017-04-20 12:17:50

标签: android

private void saveImage(Bitmap bitmap){
    try {
        String path = Environment.getExternalStorageDirectory().toString();
        Toast.makeText(getActivity(), path, Toast.LENGTH_SHORT).show();
        OutputStream fOut = null;
        File file = new File(path, mWallpaper.getId().toString() + ".jpg");
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
        fOut.flush();
        fOut.close();

        MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    } catch (MalformedURLException e) {
        Toast.makeText(getActivity(), "Error saving wallpaper. MAL", Toast.LENGTH_SHORT).show();
    } catch (IOException e1) {
        Toast.makeText(getActivity(), "Error saving wallpaper.", Toast.LENGTH_SHORT).show();
        e1.printStackTrace();
    }
}

我将此添加到我的AndroidManifests:

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

我正在尝试将图像保存到/ storage / emulated / 0 /.

修改

int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        } else {

            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    permissionCheck);
        }
    }

我已将此添加到onCreate()中,但我还是没有请求。

3 个答案:

答案 0 :(得分:4)

if (Build.VERSION.SDK_INT >= 23) {
    int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
}

如果您使用&gt; = api 23,请执行此操作。之后添加您的代码。使用此代码,您可以询问用户的运行时权限。

Requesting Permissions at Run Time Documentation from official Android site

答案 1 :(得分:1)

另一种解决方案是使用

getApplicationContext().getFilesDir().getPath()

而不是

Environment.getExternalStorageDirectory()

如果您实施此解决方案,则需要向Manifest添加任何权限,因为您的文件保存在Application目录中,而不是SD卡。

答案 2 :(得分:0)

 if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            } else {


                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},23
                );
            }
        }

试试这个你也可以关注folling链接 http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow