適配小米華為手機等拍照后獲取不到照片


問題摘要:適配小米華為手機等拍照后獲取不到照片

出現場景

普通的相機調用,在 intent 傳進去一個路徑,然調用這個意圖。

在測試機 榮耀 8X 上是沒有問題的,能獲取到拍的照片。

在小米系統和 華為麥芒4上就不行,路徑上就沒有照片。

   /**
     * @param file 拍照生成的照片地址
     * @return intent
     */
    public static Intent getTakePictureIntent(File file) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(CommonUtils.context.getPackageManager()) != null) {
            if (null != file) {
                tempPicturePath = file.getPath();
                LogUtil.log(DAUtils.class.getSimpleName(),"getTakePictureIntent  :->>"+tempPicturePath);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                } else {
                    ContentValues contentValues = new ContentValues(1);
                    contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
//                    Uri uri = CommonUtils.context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                    Uri uri = FileProvider.getUriForFile(CommonUtils.context, "xxx.fileProvider", file.getAbsoluteFile());
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                }
               
            }
        }
        return intent;
    }

出現原因

不能獲取到照片的原因是因為這個照片的目錄沒有創建。

在傳入 URI 之前要把照片的目錄給創建出來。不然就拿不到照片。

如何修復

修改如下:在傳入照片 URI 前保證目錄已經被創建

   /**
     * @param file 拍照生成的照片地址
     * @return intent
     */
    public static Intent getTakePictureIntent(File file) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(CommonUtils.context.getPackageManager()) != null) {
            if (null != file) {
             if (!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }
                tempPicturePath = file.getPath();
                LogUtil.log(DAUtils.class.getSimpleName(),"getTakePictureIntent  :->>"+tempPicturePath);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                } else {
                    ContentValues contentValues = new ContentValues(1);
                    contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
//                    Uri uri = CommonUtils.context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                    Uri uri = FileProvider.getUriForFile(CommonUtils.context, "xxx.fileProvider", file.getAbsoluteFile());
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                }
               
            }
        }
        return intent;
    }

排查過程

反復 debug 斷點,google 半小時無果后,靈光一閃,想到了。

總結

這是個坑

End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM