關於拍照返回路徑問題

近期在修改bug 發現一部紅米4測試機的拍照返回異常 因爲之前全部使用的使系統默認路徑 在測試機上拍照成功後選擇直接閃退 想了想 可能是因爲有的系統禁止使用默認路徑 也可能是返回bingder過大異常了 反正沒想通 。。。。所以直接換成 保存指定路徑了 然後在uri的獲取方法上 加上一個判斷就可以了


File file = new File(PathUtil.getFilepath("/js"), new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    cameraUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
} else {
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraUri = Uri.fromFile(file);
}

if (intent != null) {
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
    startActivityForResult(intent, RESULT_CAMERA_ACTIVITY);
}

注意到這個錯誤 android.os.FileUriExposedException bugly的

解決方案

因爲buildsdk是>=24,所以調用Uri.fromFile時保錯,解決方法:
在application的onCreate裏: StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure()
SDK>24 和<24的解決方案 public static void openFile(Context context, File file) {         Intent intent = new Intent();         intent.setAction(android.content.Intent.ACTION_VIEW);         Uri uri;         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {             intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);             Uri contentUri = FileProvider.getUriForFile(context,                     context.getApplicationContext().getPackageName() + ".provider",                     file);             intent.setDataAndType(contentUri, "application/vnd.android.package-archive");         } else {             uri = Uri.fromFile(file);             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             intent.setDataAndType(uri, "application/vnd.android.package-archive");         }         context.startActivity(intent);     }
<------------------------------分割線-----------------------------> ***************************************************************************** ***爲Android N調用相機時崩潰提供一種解決方法,親測有效*** ***其中 savePath 爲臨時保存的路徑,tempFileName爲拍照 *** ***後存儲的照片名稱。                                                           *** ************************************************************By JayGoo *** *******************************************************************************  /**      * 從拍照獲取圖片      */     public void camera() {         Intent intent = null;         // 判斷存儲卡是否可以用,可用進行存儲
        if (StorageUtils.hasSdcard()) {             //設定拍照存放到自己指定的目錄,可以先建好             File file = new File(savePath);             if(!file.exists()){                 file.mkdirs();             }             Uri pictureUri;             File pictureFile = new File(savePath, tempFileName);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {                 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                 ContentValues contentValues = new ContentValues(1);                 contentValues.put(MediaStore.Images.Media.DATA, pictureFile.getAbsolutePath());                 pictureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);             }else {                 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                 pictureUri = Uri.fromFile(pictureFile);             }             if (intent != null) {                 intent.putExtra(MediaStore.EXTRA_OUTPUT,                         pictureUri);                 startActivityForResult(intent, 1);             }         }     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章