I'm getting reports of security exceptions when trying to display images. I've found that I need to use provider to display them without the error.
At the moment I add the images using
And they are displayed with
Can anyone point me in the right direction. I need this to apply to new saved images and the existing saved ones.
At the moment I add the images using
Code:
@Override
public boolean onMenuItemClick(MenuItem item) {
String clicked = item.getTitle().toString();
if(clicked.equals("Take Photo")) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
File file = null;
try {
file = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
photoUri = null;
if (file != null) {
photoUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(getContext(),"No camera permissions.",Toast.LENGTH_SHORT).show();
}
} else {
Intent pickPhoto = new Intent(Intent.ACTION_OPEN_DOCUMENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickPhoto.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
startActivityForResult(pickPhoto , 1);
}
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
imageView.setImageURI(photoUri);
final int takeFlags = imageReturnedIntent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContext().getContentResolver().takePersistableUriPermission(photoUri, takeFlags);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
this.imageView.setImageURI(selectedImage);
photoUri = selectedImage;
final int takeFlags = imageReturnedIntent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContext().getContentResolver().takePersistableUriPermission(photoUri, takeFlags);
}
break;
}
}
And they are displayed with
Code:
@Override
public void onBindViewHolder(ProjectListViewHolder holder, int position) {
JoinProjectPicture projectPicture = mProjects.get(position);
Project current = projectPicture.getProject();
Picture picture = projectPicture.getPicture();
holder.projectName.setText(current.getProjectName() + "(" + current.getWidth() + "x" + current.getHeight() + ")");
holder.projectStatusTV.setText(current.getStatus());
if(picture != null) {
String pictureName = picture.getPictureName();
Uri uriParsed = Uri.parse(pictureName);
holder.projectStatusTV.setText("" + uriParsed);
if (ContextCompat.checkSelfPermission(act, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
holder.imageV.setImageURI(uriParsed);
}
}
}
Can anyone point me in the right direction. I need this to apply to new saved images and the existing saved ones.