Open image in gallery with Android Nougat

﹥>﹥吖頭↗ 提交于 2019-12-10 13:39:45

问题


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

That's 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 generated 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 Listener

    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 generate a wrong path for the image, but with old version it works (I tried on Marshmallow and works great).

Can someone help me? Thanks.


回答1:


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

As it stands, the other app has no rights to work with the content identified by the Uri. Adding FLAG_GRANT_READ_URI_PERMISSION does this.

This is covered in the FileProvider documentation, along with modern books on Android app development.




回答2:


I do expire the same issue with almost all apps I tried, my code is the same as yours and as far as the google docs say it looks to be correct...

The only "solution" I can offer you is to compile against version 23(https://source.android.com/source/build-numbers.html) to avoid the forced strict mode for file URIs. Of course you also have to downgrade your Android support libs in case you are using them.

If someone has a better idea please share...




回答3:


To view image from mobile emulated gallery is no more difficult but in nougat is quite different i have run time image created and wants to see that image its failed many times but finally its run with this code

1.after a long struggle i write this code , dont forget to add, fil provider in 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);

            }


来源:https://stackoverflow.com/questions/42703341/open-image-in-gallery-with-android-nougat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!