首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.io.FileNotFoundException: /storage/6CDB-8541/DCIM/Camera/IMG_20180523_202102.jpg (权限被拒绝)

java.io.FileNotFoundException: /storage/6CDB-8541/DCIM/Camera/IMG_20180523_202102.jpg (权限被拒绝)
EN

Stack Overflow用户
提问于 2018-05-28 12:02:32
回答 3查看 532关注 0票数 1

我在获取FileOutputStream的图像文件路径时遇到了问题。存在与权限相关的错误。我该如何解决这个问题呢?

代码语言:javascript
复制
if (getIntent().getExtras().getString("file_path") != null) {
    Log.d(TAG, "Path is NOT NULL");
    file = new File(getIntent().getExtras().getString("file_path"));

    Bitmap src;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getPath(), options);

    final int REQUIRED_SIZE = 400;
    int scale = 1;
    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;

    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    src = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    FileOutputStream os;

    try{
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(file.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap bmRotated = rotateBitmap(src, orientation);

        os = new FileOutputStream(file.getAbsoluteFile());
        bmRotated.compress(Bitmap.CompressFormat.JPEG, 100, os);
        photoView.setImageBitmap(bmRotated);
        os.flush();
        os.close();
    } catch (Exception e){
        e.printStackTrace();
    }
} else {
    Log.d(TAG, "Path is NULL");
}

我试着从其他来源寻找,但这对我知道答案没有帮助。

EN

回答 3

Stack Overflow用户

发布于 2018-05-28 12:06:00

你有没有在清单中声明过这个?我想你错过了android的权限。

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

票数 0
EN

Stack Overflow用户

发布于 2018-05-28 13:22:07

AndroidManifest中声明权限时,还需要处理Runtime permissions

票数 0
EN

Stack Overflow用户

发布于 2018-05-28 13:34:24

将以下权限授予android清单文件..如果执行读取和写入,则同时添加这两个..

代码语言:javascript
复制
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

然后,在活动中,当您执行读写操作时,该定时器将处理运行时权限,如下所示。

代码语言:javascript
复制
 /**
 * this method check permission and return current state of permission need.
 */
private boolean checkPermissions() {
    int permissionState = ActivityCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE);
    return permissionState == PackageManager.PERMISSION_GRANTED;
}
  /**
 * this method request to permission asked.
 */

private void requestPermissions() {
    boolean shouldProvideRationale =
            ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE);

    if (shouldProvideRationale) {
    } else {
        Log.i("Error", "Requesting permission");
        // previously and checked "Never ask again".
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_PERMISSIONS_REQUEST_CODE);
    }
}

然后执行您的操作。

代码语言:javascript
复制
 if (!checkPermissions()) {
        requestPermissions();
    } else {
        // here your operation 
        getFileDetails();

    }

当权限被授予时,则在下面的方法中获得回调。

代码语言:javascript
复制
   @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length <= 0) {
            // If user interaction was interrupted, the permission request is cancelled and you
            // receive empty arrays.
            Log.i("Error", "User interaction was cancelled.");
        } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission was granted. Kick off the process of building and connecting
            // GoogleApiClient.
        } else {
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50558951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档