从相机拍照后,我尝试从 URI 获取真实文件路径。我这样做:
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
和这个:
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaColumns.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
和:
private String getRealPathFromURI(Uri selectedVideoUri) {
String filePath;
final String[] filePathColumn = new String[] { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(selectedVideoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
在大多数手机上,一切正常。但在某些手机应用程序崩溃并出现错误:
08-03 14:55:13.092: E/AndroidRuntime(1676): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(SourceFile:190)
08-03 14:55:13.092: E/AndroidRuntime(1676): at android.os.Handler.dispatchMessage(SourceFile:130)
08-03 14:55:13.092: E/AndroidRuntime(1676): at android.os.Looper.loop(SourceFile:351)
08-03 14:55:13.092: E/AndroidRuntime(1676): at android.app.ActivityThread.main(SourceFile:4070)
08-03 14:55:13.092: E/AndroidRuntime(1676): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:55:13.092: E/AndroidRuntime(1676): at java.lang.reflect.Method.invoke(Method.java:538)
08-03 14:55:13.092: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(SourceFile:901)
08-03 14:55:13.092: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit.main(SourceFile:659)
08-03 14:55:13.092: E/AndroidRuntime(1676): at dalvik.system.NativeStart.main(Native Method)
08-03 14:55:19.823: I/System.out(1728): --MyLocationListener is created!
08-03 14:55:19.838: D/AndroidRuntime(1728): Shutting down VM
08-03 14:55:19.838: W/dalvikvm(1728): threadid=1: thread exiting with uncaught exception (group=0x4001c648)
08-03 14:55:19.842: E/AndroidRuntime(1728): FATAL EXCEPTION: main
08-03 14:55:19.842: E/AndroidRuntime(1728): java.lang.RuntimeException: Unable to create service com.webparadox.tonquer.MyLocationListener: java.lang.NullPointerException
08-03 14:55:19.842: E/AndroidRuntime(1728): at android.app.ActivityThread.handleCreateService(SourceFile:2221)
08-03 14:55:19.842: E/AndroidRuntime(1728): at android.app.ActivityThread.access$2500(SourceFile:160)
请帮我修复这个错误