java.lang.NullPointerException:尝试调用虚拟方法“java.lang.String android.net.Uri.getScheme()”
我正在尝试从gallery和camera将图像上载到服务器。当我从gallery选择图像以将其上载到服务器时,其工作正常。但当我选择camera captured image以上载到服务器时,其崩溃并显示错误java.lang.NullPointerException:尝试调用虚拟方法“java.lang.String android.net.Uri.getScheme()”,java,android,image,upload,server,Java,Android,Image,Upload,Server,我正在尝试从gallery和camera将图像上载到服务器。当我从gallery选择图像以将其上载到服务器时,其工作正常。但当我选择camera captured image以上载到服务器时,其崩溃并显示错误 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference 在OnAc
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
在OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
decodeFile(picturePath);
new ImageUploadTask().execute();
}else {
Toast.makeText(getApplicationContext(), "User Canceled",
Toast.LENGTH_LONG).show();
}
}
我在onActivityResult中得到了这一行的错误
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
我在点击按钮时打开相机时使用的方法
loadimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(Add_Info.this);
builder.setMessage("Select Image From")
.setCancelable(true)
.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg"));
}
})
.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
帮我解决这个问题。我不知道如何得到这个错误,即使我使用了已经在我以前的项目
提前感谢我在使用摄像头api和onActivityResult的意图时遇到了同样的问题,最后我发现这取决于您的android设备版本,在android M Marshmallow-6.0–6.0.1中,onActivityResult的意图如下所示:
requestCode : 230 resultCode : -1 intent :Intent{dat=file:///storage/emulated/0/Pictures/Rahnama/IMG_20160508_121332_1519065 564.jpg typ=image/jpeg } RESULT_OK : -1
对于低于棉花糖的android版本:
requestCode : 230 resultCode : -1 intent : Intent { dat=content://media/external/images/media/309 (has extras) } RESULT_OK : -1
我认为您必须在ActivityResult上检查android版本和android版本棉花糖,以从Uri和Intent中的路径获取直接文件:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M){
final Uri data = intent.getData();
final File file = new File(data.getPath());
// now you can upload your image file
}else{
// in android version lower than M your method must work
}
我希望它能对您有用。我也遇到了这个问题,经过多次测试,我发现了它,因为我设置了名为android:windowIsTranslucent=true的属性,如果我从样式中删除这个属性,它总是能工作。data.getData;正在返回Null您应该发布PICK_IMAGE而不是CAMERA_Request的代码请发布整个堆栈跟踪,并指出堆栈跟踪中的哪些行与代码段中的行相对应。我没有让您@Qamarty调试您的代码,data.getData或data将为Null。您可以深入探究此答案吗??
if (null != account.getPhotoUrl()) {
// Write code here
}