Open image in gallery with Android Nougat

I want to open the saved image in the gallery on Android Nougat, but what I get is a black page of the gallery with the message "Unable to load photo."

What is my code:

manifest

<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> 

provider_paths.xml

 <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths> 

Path created in DrawView

 public static boolean save(Bitmap bitmap){ Date now = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy'_'HH:mm"); File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Crash"); if (!folder.exists()) { folder.mkdirs(); } FileOutputStream fos = null; try { lastImagePath = new File(Environment.getExternalStorageDirectory().toString() + "/Crash/" + simpleDateFormat.format(now) + ".jpg"); fos = new FileOutputStream(lastImagePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); fos = null; return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (fos != null) { try { fos.close(); } catch (IOException e) {} } } } 

Open image receiver

  private class OpenImageListener implements View.OnClickListener{ @Override public void onClick(View v) { if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + DrawView.getLastImagePath().getAbsolutePath()), "image/*"); startActivity(intent); } else { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri photoUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", DrawView.getLastImagePath()); intent.setData(photoUri); startActivity(intent); } } } 

Maybe I'm creating the wrong path for the image, but it works with the old version (I tried on Marshmallow and it works fine).

Can someone help me? Thanks.

+6
source share
3 answers

In the else block in onClick() , after calling setData() on the Intent , to set Uri , call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) on the Intent .

Otherwise, another application is not allowed to work with content identified by Uri . Adding FLAG_GRANT_READ_URI_PERMISSION does this.

This is described in the FileProvider documentation , as well as in modern books on Android application development.

+9
source

I really expire the same problem with almost all the applications I tried, my code is the same as yours, and as far as google docs say it looks right ...

The only โ€œsolutionโ€ I can offer you is to compile against version 23 ( https://source.android.com/source/build-numbers.html ) to avoid forced strict mode for URI files. Of course, you will also have to downgrade your Android support files if you use them.

If anyone has a better idea, please share ...

0
source

To view the image from the mobile emulated gallery is not more difficult, but in the nougat itโ€™s completely different, I have a runtime image, and I want this image to be skipped many times, but finally, launch it using this code

1.After a long struggle, I write this code, do not forget to add, register the provider in the mainfest file, as you may know.

  MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(ext); if (type == null) type = "*/*"; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // only for gingerbread and newer versions Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri photoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file); intent.setDataAndType(photoUri, type); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(intent); } else{ Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(file); intent.setDataAndType(data, type); context.startActivity(intent); } 
0
source

Source: https://habr.com/ru/post/1015632/


All Articles