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