Java 权限拒绝:重新启动应用程序时打开提供商com.android.providers.media.MediaDocumentsProvider

Java 权限拒绝:重新启动应用程序时打开提供商com.android.providers.media.MediaDocumentsProvider,java,android,android-studio,permissions,android-sqlite,Java,Android,Android Studio,Permissions,Android Sqlite,我有一个Android应用程序,允许用户为其个人资料选择照片,如下所示: public void pickImage(View view) { Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); getIntent.setType("image/*"); Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStor

我有一个Android应用程序,允许用户为其个人资料选择照片,如下所示:

public void pickImage(View view) {
    Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getIntent.setType("image/*");

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");

    Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

    startActivityForResult(chooserIntent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Context context = getApplicationContext();
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        try {
            ImageView im = findViewById(R.id.circularImageView);
            uri = data.getData();
            im.setImageURI(uri);
            String image = uri.toString();


            Log.d("ImageS", image);

            SqlHelper db = new SqlHelper(this);
            Intent i = getIntent();
            String userid = i.getStringExtra("Name");
            db.updateImage(userid, image);

        }catch (Exception e){

        }
        //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
    }
}
在我重新启动应用程序之前,一切正常。在这里,我想从数据库中检索图像,一切正常:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    setContentView(R.layout.activity_main);

    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MANAGE_DOCUMENTS};
    for(String permission: permissions){
        if (ContextCompat.checkSelfPermission(this, permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    permission)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                        permissions, 0);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }



    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    ImageView logo = findViewById(R.id.imageView4);
    logo.setImageResource(R.drawable.logo);
    SqlHelper db = new SqlHelper(this);

    //Add some locations for the users
    a.setLatitude(Latitude[0]);
    a.setLongitude(Longitude[0]);
    b.setLatitude(Latitude[1]);
    b.setLongitude(Longitude[1]);

    Intent intentR = getIntent();
    String name = intentR.getStringExtra("Name");


    // Check for the freshest data.

    ImageView im1 = findViewById(R.id.circularImageView2);
    im1.setImageURI(null);
    String iS = db.getImage(name);
    Log.d("ImageR", iS);
    //
    // Uri uri = Uri.parse(image);
    //Log.d("Equals", String.valueOf(iS.equals(String.valueOf(R.drawable.imageuser))));
    if(iS.equals(String.valueOf(R.drawable.imageuser))){
        im1.setImageResource(Integer.parseInt(iS));
    }else{
        Uri uri = Uri.parse(iS);
        ContentResolver contentResolver= getContentResolver();
        ParcelFileDescriptor parcelFileDescriptor = null;
        try {
//HERE COMES THE PROBLEM//
            parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r"); /
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        try {
            parcelFileDescriptor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        im1.setImageBitmap(image);
    }


    ArrayList<String> notifications = db.getNotifications(name);
    if(notifications.size()>0) {
        ImageView im = findViewById(R.id.bell);
        ImageView c = findViewById(R.id.circleN);
        TextView tx = findViewById(R.id.nNot);
        im.setVisibility(View.VISIBLE);
        c.setVisibility(View.VISIBLE);
        tx.setVisibility(View.VISIBLE);
        tx.setText(String.valueOf(notifications.size()));
    }

   // db.addUser(new User("Babouche", "Babouche", location[1]), this);
   // db.addUser(new User("Arturo", "Arturo", location[0]), this);


}`
我为我的应用程序设置了读取内部存储的权限,但它在重新启动后似乎无法工作

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturopavon.finalproject">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
    android:name="android.permission.MANAGE_DOCUMENTS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="BiteME"
    android:roundIcon="@drawable/logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
    <activity android:name=".UserActivity" />
    <activity android:name=".UserPreferences" />
    <activity android:name=".SignUp" />
    <activity android:name=".Notifications" />

</application>
关于如何解决这个问题有什么想法吗?

如SAF中所述:

如果您希望应用程序只需读取/导入数据,请使用操作获取内容。通过这种方法,应用程序导入数据的副本,例如图像文件

如果您希望应用程序能够长期、持久地访问文档提供商拥有的文档,请使用ACTION_OPEN_DOCUMENT

看起来您有了第二个用例

因此,不是:

Intent getIntent=新的意图(Intent.ACTION\u GET\u CONTENT)

试一试

Intent getIntent=新的意图(Intent.ACTION\u OPEN\u文档)


这些文档提供了使用此文档的权限。

您尚未声明
操作\u打开\u文档
权限,谢谢您的回答。我该怎么设置呢?因为我一直在寻找,但一直没有找到答案。我认为仅仅通过添加管理文档的权限就足够了。谢谢你的回答。问题是,我遇到的问题不在我选择图像的活动中。问题出现在这一行的主要活动中(一旦我通过停止应用程序重新启动了它):parcelFileDescriptor=contentResolver.openFileDescriptor(uri,“r”);在那里,我没有试图做任何意图,我得到了错误,所以我不知道如何使应用程序从提供商那里获取内容。如果我不重新启动应用程序,当从数据库中检索时,一切都会完美无瑕地工作,但之后它就会崩溃。澄清一下。如果我设置了打开文档的意图,并且我从内部存储中选择了一个图像,那么问题就消失了。例如,当我尝试从谷歌照片中挑选图像时(当我重新启动应用程序时),问题就出现了。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturopavon.finalproject">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
    android:name="android.permission.MANAGE_DOCUMENTS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="BiteME"
    android:roundIcon="@drawable/logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
    <activity android:name=".UserActivity" />
    <activity android:name=".UserPreferences" />
    <activity android:name=".SignUp" />
    <activity android:name=".Notifications" />

</application>
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.login);
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MANAGE_DOCUMENTS};
    for(String permission: permissions){
        if (ContextCompat.checkSelfPermission(this, permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    permission)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                        permissions, 0);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }

}