当前位置: 首页 > 知识库问答 >
问题:

我试图使用BitmapFactory查看/storage/emulated/0/pictures中的图像,但请参见错误权限被拒绝[重复]

赵光赫
2023-03-14
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/my_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        />
package mrppanther.com.example.read_image;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.File;

public class MainActivity extends AppCompatActivity {
    private int STORAGE_PERMISSION_CODE = 1;
    private int new_state = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // NOTE: use a button to request activity
        Button buttonRequest = findViewById(R.id.button);
        buttonRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // NOTE: declare "myfile", this is want I want to view
                File myfile = new File("/storage/emulated/0/Pictures/henry1.jpg");

                // NOTE: on the 1st click, check permissions
                if (new_state == 1) {
                    requestStoragePermission();
                    new_state = 2; // NOTE: move to the next state, on the next click

                // NOTE: on the 2nd click, view image
                } else {

                    // NOTE: check that the "myfile"" exists
                    if (myfile.exists()) {
                        Log.i("myfile", "FILE EXISTS PASS! " + myfile);
                    } else {
                        Log.i("myfile", "FILE ABSENT FAIL! " + myfile);
                    }

                    // NOTE: declare "myimage"
                    ImageView myimage = (ImageView) findViewById(R.id.my_image);

                    // NOTE: experimenting with options did not help
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    //options.inJustDecodeBounds=true;
                    options.inSampleSize=1;

                    // ERROR: E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException:
                    // ERROR: /storage/emulated/0/Pictures/henry1.jpg: open failed: EACCES (Permission denied)
                    Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(myfile), options);

                    myimage.setImageBitmap(bitmap);

                    // NOTE: here is a test to check "myimage" is correct
                    // NOTE: this works OK, and image from drawable resource is displayed
                    // myimage.setImageResource(R.drawable.henry1);

                    new_state = 1; // NOTE: go back to the 1st state, on the next click
                }
            }
        }); // NOTE: this is the end of button activity
    }

    // NOTE: request permission to read storage, at run time
    private void requestStoragePermission() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
        // NOTE: here is a test which sets the wrong permission
        // new String[]{Manifest.permission.READ_PHONE_NUMBERS,Manifest.permission.READ_PHONE_STATE}, STORAGE_PERMISSION_CODE);
    }

    // NOTE: check read permission is granted, at run time
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

结果:我看到下面的错误消息,图像没有显示。但是,运行时权限检查指示“已授予权限”。最后我重新启动了我的平板电脑。我检查了应用程序是否允许访问存储(在设置,应用程序,存储权限)。我在USB断开的情况下重新运行应用程序,但图像没有显示。

I/myfile: FILE EXISTS PASS! /storage/emulated/0/Pictures/henry1.jpg
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/henry1.jpg: open failed: EACCES (Permission denied)

共有1个答案

谷梁驰
2023-03-14

在Android10及以上版本中,您只能对位于本地私有目录中的媒体使用File类。不能使用文件路径直接访问外部媒体。

当涉及到公共媒体时,必须使用媒体存储区中的实际媒体Uri。对于您的具体情况,请执行以下操作:

  1. 使用媒体存储获取目标图像Uri
  2. 打开以这样的URI为目标的流。
  3. 将流与BitMapFactory一起使用。
  4. 不要忘记关闭流。
 类似资料:
  • 问题内容: 我正在尝试使用android中的异步任务下载和存储图像,当单击下载按钮时出现以下错误。 WallpaperActivity.java Android目标Api 7.1.1 清单中添加的权限 android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE 问题答案: 您需要为OS Mar

  • 我正试图在android中使用异步任务下载和存储图像,当点击下载按钮时得到以下错误。 null Android.permission.write_external_storage

  • 我正在尝试一个非常基本的相机应用程序的源代码在Android Studio 3.4.1与Android虚拟设备(AVD)Nexus 7(2012)API 29.我用模拟器中的相机拍了一张照片,并试图将其保存为 /storage/emulated/0/Pictures/myPic.png,但 /storage/emulated/0/Pictures/myPic.png打开失败:EACCES(拒绝许可

  • 我尝试使用以下方法从URI获取路径: 当我尝试压缩位图时: 我得到这个错误:

  • 问题内容: 我一直在尝试加密文件,并将这些文件写回到同一位置。但是我收到错误消息说 我的档案是这个 我认为我已经提供了正确的许可。我用来加密文件的代码就是这个。 我在按钮内使用了这种方法 仍然我无法配置此错误。请有人帮忙! 问题答案: 我怀疑您运行的是Android 6.0棉花糖(API 23)或更高版本。在这种情况下, 必须先 实现运行时权限,然后才能尝试读取/写入外部存储。

  • 问题内容: 我运行 npm install lodash, 但它抛出 错误:EACCES:权限被拒绝 错误。我知道这是权限问题,但据我所知,本地安装节点模块不需要sudo权限。如果我使用sudo运行它,它将安装在〜/ node_modules文件夹中。 drwxrwxr-x 是现有文件夹的文件许可权。我不知道可能出了什么问题。 下面是错误消息。 问题答案: 使用 npm init 创建 packa