【发布时间】:2016-04-17 10:34:00
【问题描述】:
嘿,我已经尝试解决这个问题几个小时,并广泛检查了许多其他问题,试图解决这个问题。
所以在我的主要活动中,我有两个活动,它们要么从图库中抓取图像,要么根据按钮拍摄照片
public void DoTakePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, TAKE_PICTURE);
}
}
public void DoShowSelectImage(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
}
所以我知道问题就像我的 onActivityResult 数据似乎为 null 我尝试使用 super 以便它找回丢失的活动或检查数据是否为空
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE || requestCode == TAKE_PICTURE && null != data) {
if (resultCode == RESULT_OK && null != data) {
Uri selectedimage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedimage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mImageFullPathAndName = cursor.getString(columnIndex);
cursor.close();
jobID = "";
File file = new File(mImageFullPathAndName);
Bitmap mCurrentSelectedBitmap = decodeFile(file);
if (mCurrentSelectedBitmap != null) {
ivSelectedImg.setImageBitmap(mCurrentSelectedBitmap);
int w = mCurrentSelectedBitmap.getWidth();
int h = mCurrentSelectedBitmap.getHeight();
int length = (w > h) ? w : h;
if (length > OPTIMIZED_LENGTH) {
float ratio = (float) w / h;
int newW, newH = 0;
if (ratio > 1.0) {
newW = OPTIMIZED_LENGTH;
newH = (int) (OPTIMIZED_LENGTH / ratio);
} else {
newH = OPTIMIZED_LENGTH;
newW = (int) (OPTIMIZED_LENGTH * ratio);
}
mCurrentSelectedBitmap = rescaleBitmap(mCurrentSelectedBitmap, newW, newH);
}
mImageFullPathAndName = SaveImage(mCurrentSelectedBitmap);
}
}
}
}
所以我的错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.zola.capchem/com.example.zola.capchem.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
我已经尝试了这个网站上的大部分内容,但应用程序仍然崩溃。不知道在这里做什么。
【问题讨论】:
-
在启动 Activity 之前,将 imageURI 设置为 extras
-
@Jhaman Das 我刚刚尝试过,它返回了同样的错误
标签: java android nullpointerexception crash