我遇到了一些与Media.insertImage方法有关的奇怪问题 https://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#insertImage(android.content.ContentResolver,%20android.graphics.Bitmap,%20java.lang.String,%20java.lang.String)
private Uri createTemporaryUri(Bitmap bitmap)
{
fixMediaDirForKitkat();
String savedPath = MediaStore.Images.Media.insertImage(requestTarget.getMyFragment().getActivity().getContentResolver(), bitmap, "someone_tmp", null);
return Uri.parse(savedPath);
}
private void fixMediaDirForKitkat()
{
File sdcard = Environment.getExternalStorageDirectory();
if(sdcard != null) {
File mediaDir = new File(sdcard, "DCIM/Camera");
if( !mediaDir.exists()) {
mediaDir.mkdirs();
}
}
}
对于某些不使用方法fixkiaDirForKitkat的kitkat设备(4.4 - 4.4.4),insertImage函数有时会返回null。添加方法后,我遇到了:
Caused by java.lang.IllegalStateException: Unable to create new file: /storage/sdcard0/DCIM/Camera/1480524677437.jpg
android.provider.MediaStore$Images$Media.insertImage (MediaStore.java:1008)
some.one.PhotoUploader.createTemporaryUri (PhotoUploader.java:166)
some.one.PhotoUploader.beginImageUpload (PhotoUploader.java:145)
some.one.registration.RegisterFragment.onActivityResult (RegisterFragment.java:107)
还有什么需要初始化吗?这里有任何黑客或解决方法吗?设备没有SD卡/空间?无法创建新文件告诉我什么; - )
高度赞赏任何建议
答案 0 :(得分:0)
如果您未授予权限,请授予您的Android清单文件权限,如果忽略它。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您想创建目录,然后按照此代码
public static String downloadPath = "/DCIM/Camera";
File fileDir = new File(downloadPath);
if (fileDir.isDirectory()) {
request.setDestinationInExternalPublicDir(downloadPath, filename);
} else {
fileDir.mkdirs();
request.setDestinationInExternalPublicDir(downloadPath, filename);
}
或强> 问题似乎与Android 6.0中引入的Android Runtime Permission相关
当您的应用定位API级别23时,默认情况下所有权限均为false,您必须先申请权限对话框并批准该权限,然后再将其用于您的应用。