java.lang.IllegalStateException无法创建新文件:/storage/sdcard0/DCIM/Camera/1509245737788.jpg

时间:2017-12-29 09:25:30

标签: android

它发生在我拍照时,WRITE_EXTERNAL_STORAGE在Manifest中,是Android 4.2.1,它只发生在小米设备上。这是takePhoto()完整代码。

    public static Uri takePhoto(Context context, final String fileName, File path){
    Intent intent = null;
    Uri uri= null;
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
    String state = Environment.getExternalStorageState();  
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        file = new File(path,fileName);
        if(file.exists()){
            file.delete();
        }
        try {
            path.mkdirs();
        }catch (Exception e){
            Utils.showToast(context,"创建路径异常");
            Utils.isLogError("FilePath","创建路径异常:"+Log.getStackTraceString(e),isLog);
        }
        if (currentapiVersion < 24) {
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Images.Media.TITLE,fileName);
            uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        }else {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            uri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        }
        ((Activity) context).startActivityForResult(intent,CAMERA_REQUEST_CODE);
    }else{
        Utils.showToast(context,"SD卡未挂载");
    }
    return uri;
}

这是日志:

Unable to create new file: /storage/sdcard0/DCIM/Camera/1509245804437.jpg
android.os.Parcel.readException(Parcel.java:1433)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
android.content.ContentResolver.insert(ContentResolver.java:936)
com.sdj.wallet.util.Utils.takePhone(Utils.java:1727)
com.sdj.wallet.activity.AuthenticatioIdentityCardPicActivity.onClick(AuthenticatioIdentityCardPicActivity.java:402)

0 个答案:

没有答案