其他分享
首页 > 其他分享> > 即使使用用户权限,也无法在外部存储上保存文件[Android]

即使使用用户权限,也无法在外部存储上保存文件[Android]

作者:互联网

我正在为Android上的图像处理开发一个应用程序,但我仍然在编写用于保存图像的代码.

这是我使用的方法:

private void saveImageToExternalStorage(Bitmap finalBitmap) {
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }


    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });

}

每次执行此操作时,FileOutputStream都会给出FileNotFoundException.

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gabrielerestuccia.testimmagini">

<application
    ...
</application>

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>

我正在Linux上的Android Studio 1.5.1上开发这个,使用内置的虚拟Nexus 5和Android 6.0.

谢谢您的帮助.

解决方法:

我在Android 6上遇到了同样的问题.这是因为新的权限模型.

如果您拥有以下代码段的权限,则可以动态检查:

private static boolean canWriteToExternalStorage(Context context) {
    return ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}

您还可以转到设置 – >为您的应用启用写入权限.应用 – >你的应用 – >应用权限并启用存储项目.

为了向您的用户询问此权限,您应该在Activity中添加以下代码:

public void onCreate(Bundle savedInstanceState) {
    ...
    checkWritingPermission();
}

@Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CODE_PERMISSION) {
      if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // permission was granted
      } else {
        // permission wasn't granted
      }
    }
  }

  private void checkWritingPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // permission wasn't granted
      } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
      }
    }
  }

标签:android,android-intent,android-bitmap
来源: https://codeday.me/bug/20190608/1201156.html