简体   繁体   English

Android打开失败:EACCES(权限被拒绝)

[英]Android open failed: EACCES (Permission denied)

I'm trying to pick an image from the gallery and when i pick it i get this error: 我正在尝试从图库中选取一张图片,当我选取它时出现此错误:

open failed: EACCES (Permission denied) 

in the manifest i already set the permissions: 在清单中,我已经设置了权限:

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

But i still have the error and i can't view the image in the ImageView. 但是我仍然有错误,并且无法在ImageView中查看图像。 This is the code anyway: 无论如何这是代码:

public void loadImagefromGallery() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.firstImg);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }

where loadImagefromGallery() method is called in a button on click. 单击时在一个按钮中调用loadImagefromGallery()方法。 I can't understand the problem.. 我不明白问题所在。

EDIT: 编辑:

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_CODE_ASK_PERMISSIONS);
                    return;
                }
                loadImagefromGallery();

            }
        });

This is my onClick() method. 这是我的onClick()方法。 i added the request for permission but still get the same error 我添加了许可请求,但仍然收到相同的错误

SOLVED: I want to post my way to solve the problem. 已解决:我想发布解决问题的方法。 This is what i've done: 这是我所做的:

private void insertDummyImageWrapper() {
    int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
        if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            showMessageOKCancel("You need to allow access to SdCard",
                new DialogInterface.OnClickListener() {
            @Override
                public void onClick(DialogInterface dialog, int which) {
                    requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_CODE_ASK_PERMISSIONS);
                }
            });
            return;
        }
        requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
        REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
    loadImagefromGallery();
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(MainActivity.this)
        .setMessage(message)
        .setPositiveButton("OK", okListener)
        .setNegativeButton("Cancel", null)
        .create()
        .show();
}

And in the fab i called the insertDummyImageWrapper() method 在工厂中,我调用了insertDummyImageWrapper()方法

fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                REQUEST_CODE_ASK_PERMISSIONS);
                        return;
                    }
                    insertDummyImageWrapper();

                }
            });

Now it 's working perfectly 现在一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM