Matisse: 小米8 Android10.0版本拍照错误及其临时解决方案

Created on 10 Apr 2020  ·  8Comments  ·  Source: zhihu/Matisse

手机: 小米8
版本:Android10.0
现象:拍照后,返回来图片选择页面,并没有返回自己的页面
使用代码:Matisse演示代码
现有代码:
AndroidMinifest.xml中采取如下:

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.zhihu.matisse.sample.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public"/>
        </provider>

调查原因:拍照后返回的resultCode为0,而不是-1

临时解决方案(需要下载源码进行解决):
AndroidMinifest.xml采取如下方案:
xml中采取如下:

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.zhihu.matisse.sample.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_private"/>
        </provider>

MediaStoreCompat.java
注意注释的地方

...
 @SuppressWarnings("ResultOfMethodCallIgnored")
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp =
                new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
        File storageDir;
//        if (mCaptureStrategy.isPublic) {
//            storageDir = Environment.getExternalStoragePublicDirectory(
//                    Environment.DIRECTORY_PICTURES);
//            if (!storageDir.exists()) storageDir.mkdirs();
//        } else {
            storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//        }
        if (mCaptureStrategy.directory != null) {
            storageDir = new File(storageDir, mCaptureStrategy.directory);
            if (!storageDir.exists()) storageDir.mkdirs();
        }

        // Avoid joining path components manually
        File tempFile = new File(storageDir, imageFileName);

        // Handle the situation that user's external storage is not ready
        if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
            return null;
        }

        return tempFile;
    }
...

Most helpful comment

粗暴一点,AndroidManifes.xml里面的application增加android:requestLegacyExternalStorage="true"

All 8 comments

这不是最好的解决方案....,仅供有需要的进行参考...

访问系统媒体文件:Q中引入了一个新定义媒体文件的共享集合,如果要访问沙盒外的媒体共享文件,比如照片,音乐,视频等,需要申请新的媒体权限:READ_MEDIA_IMAGES,READ_MEDIA_VIDEO,READ_MEDIA_AUDIO,申请方法同原来的存储权限。

这个应该是Android Q的权限问题,我把targetSdkVersion改到29以下就没问题了,应该是没权限,但是新增的READ_MEDIA_IMAGES权限在29的SDK里并没有,没办法申请,很迷

兄弟,厉害啊,当时我只想赶紧让这个功能好使...

粗暴一点,AndroidManifes.xml里面的application增加android:requestLegacyExternalStorage="true"

粗暴一点,AndroidManifes.xml里面的application增加android:requestLegacyExternalStorage="true"

嗯,目前来看,这个方法比较好一些:-(

@10YearsDiary @wjyin Android 11 上这个属性直接失效,所以还是找解决方法吧

这个方法是可行的,就是不知道会不会有别的隐患。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

souragar picture souragar  ·  4Comments

HuRuWo picture HuRuWo  ·  4Comments

zouzhenglu picture zouzhenglu  ·  4Comments

cv0cv0 picture cv0cv0  ·  7Comments

Alastor111 picture Alastor111  ·  6Comments