我是新的Android,在这个应用程序中,你需要填写一些编辑文本,如姓名,电子邮件等,然后使用按钮打开图库或另一个拍照的按钮添加图片,然后你有按下按钮将此信息发送到数据库。它可以工作,如果我从图库添加图片,并发送此信息,但当我做相同的拍照时应用程序崩溃。我不确定会出现什么问题,我真的很感谢有人帮助我。或者告诉我可能导致这个问题的原因。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK ){
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), (filePath));
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, filePath));
} catch (IOException e) {
e.printStackTrace();
}
}else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
areaLeyendaImagen.setVisibility(View.VISIBLE);
areaFotoCargada.setVisibility(View.VISIBLE);
imagencargada.setVisibility(View.VISIBLE);
textoimagencargada.setVisibility(View.VISIBLE);
area_cerrarIagen.setVisibility(View.VISIBLE);
cerrarImagen.setVisibility(View.VISIBLE);
buttonChoose.setError(null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPath(Uri uri) {
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getActivity().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
logcat的:
08-20 23:23:53.320 11022-11022/com.example.lupitagarcia.yosoyvallarta E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lupitagarcia.yosoyvallarta, PID: 11022
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at com.example.lupitagarcia.yosoyvallarta.TerminosYC.getPath(TerminosYC.java:579)
at com.example.lupitagarcia.yosoyvallarta.TerminosYC$1.onClick(TerminosYC.java:355)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10936)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
if(mCursor!=null && mCursor.getCount()>0 ){
mCursor.moveToFirst();
}
原因是你的#uri,因为你在那里获得空值。 首先要确保你是否正在获得正确的图像!