try{
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imgPath,
filePathColumn, null, null, null);
if(cursor.getCount()>0){
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex); }
else{
Toast.makeText(getApplicationContext(), "Cursor Null", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
e.printStackTrace();
}
Log Cat:
04-26 14:07:12.920: W/System.err(18580): java.lang.NullPointerException
04-26 14:07:12.930: W/System.err(18580): at com.example.camera.CameraAct$10$1.run(CameraAct.java:365)
04-26 14:07:12.930: W/System.err(18580): at android.os.Handler.handleCallback(Handler.java:587)
04-26 14:07:12.930: W/System.err(18580): at android.os.Handler.dispatchMessage(Handler.java:92)
04-26 14:07:12.930: W/System.err(18580): at android.os.Looper.loop(Looper.java:132)
04-26 14:07:12.930: W/System.err(18580): at android.app.ActivityThread.main(ActivityThread.java:4126)
04-26 14:07:12.930: W/System.err(18580): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 14:07:12.930: W/System.err(18580): at java.lang.reflect.Method.invoke(Method.java:491)
注意:我不知道游标中的null pointerexception是什么原因。我将这段代码用于三星galaxy s3。
答案 0 :(得分:1)
Cursor cursor = getContentResolver().query(imgPath,
filePathColumn, null, null, null);
if(cursor.getCount()>0){
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex); }
else{
Toast.makeText(getApplicationContext(), "Cursor Null", Toast.LENGTH_SHORT).show();
}
显然getContentResolver().query
返回一个空游标。您需要确保query(imgPath,filePathColumn, null, null, null)
正确无误,特别是imgPath