[Java for Android App Development] Pavononer23:
Hello everyone, I have an Android application that allows users to choose a photo for their profile as follows:

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);

}

[MENTION=439709]override[/MENTION]

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...

}

}

Everything works fine and when I want to retrieve from my db the image everything works fine:

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))){ //Part to determine whether the user changed it or I use the default one

im1.setImageResource(Integer.parseInt(iS));

}else{

Uri uri = Uri.parse(iS);

ContentResolver contentResolver= getContentResolver();

ParcelFileDescriptor parcelFileDescriptor = null;

try {

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);

}

The thing is that when I restart the application, I get the following error when going into an activity in which a user changed the image:

FATAL EXCEPTION: main

Process: com.example.arturopavon.finalproject, PID: 1445

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arturopavon.finalproject/com.example.arturopavon.finalproject.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{47cb71a 1445:com.example.arturopavon.finalproject/u0a294} (pid=1445, uid=10294) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)

at android.app.ActivityThread.-wrap11(Unknown Source:0)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)

at android.os.Handler.dispatchMessage(Handler.java:106)

at android.os.Looper.loop(Looper.java:164)

at android.app.ActivityThread.main(ActivityThread.java:6501)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{47cb71a 1445:com.example.arturopavon.finalproject/u0a294} (pid=1445, uid=10294) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

at android.os.Parcel.readException(Parcel.java:2004)

at android.os.Parcel.readException(Parcel.java:1950)

at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4827)

at android.app.ActivityThread.acquireProvider(ActivityThread.java:5843)

at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2526)

at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1783)

at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396)

at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1249)

at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1102)

at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1056)

at com.example.arturopavon.finalproject.MainActivity.onCreate(MainActivity.java:126)

at android.app.Activity.performCreate(Activity.java:7026)

at android.app.Activity.performCreate(Activity.java:7017)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

I have permissions set for my app to read internal storage but it is like it is not working after rebooting.

<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"/>

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.

}

}

}

Any ideas on how to solve this?