错误:
致命的例外:主要 处理:ua.romanpotapskiy.antihawk.prokaton,PID:22721 java.lang.IllegalArgumentException:无效的URI:file:///storage/emulated/0/Pictures/P/B/JPEG_20160115_170005_776674531.jpg 在android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:752) at ua.romanpotapskiy.antihawk.prokaton.RealPathUtil.getRealPathFromURI_API19(RealPathUtil.java:19) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.createImageFile(DeliverActivity.java:428) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.dispatchTakePictureIntent(DeliverActivity.java:298) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.onClick(DeliverActivity.java:248) 在android.view.View.performClick(View.java:4780) 在android.view.View $ PerformClick.run(View.java:19866) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5258) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我的代码:
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Prokaton/Big");
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
if (Build.VERSION.SDK_INT < 11)
mCurrentPhotoPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, Uri.fromFile(image));
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
mCurrentPhotoPath = RealPathUtil.getRealPathFromURI_API11to18(this, Uri.fromFile(image));
// SDK > 19 (Android 4.4)
else
mCurrentPhotoPath = RealPathUtil.getRealPathFromURI_API19(this, Uri.fromFile(image));
...
}
RealPathUtilClass:
公共类RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
我试图使用这个exaple http://hmkcode.com/android-display-selected-image-and-its-real-path/ 但我不需要这个:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// 2. pick image only
intent.setType("image/*");
// 3. start activity
startActivityForResult(intent, 0);
答案 0 :(得分:1)
尝试在您的方法中使用此代码(对所有API版本使用相同的代码)
希望它会帮助你
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor == null) {
result = uri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (cursor != null) {
cursor.close();
}
}
return result;