Rus*_*oid 5 java upload android image server
我正在尝试将图像从图库和相机上传到服务器。当我从图库中选择图像上传到服务器时,它的工作 Perfactly。但是当我选择相机捕获的图像上传到服务器时,它崩溃并显示错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
Run Code Online (Sandbox Code Playgroud)
在 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我在 onActivityResult 的这一行收到错误
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
Run Code Online (Sandbox Code Playgroud)
我用于在按钮单击时打开相机的方法。
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();
}
});
Run Code Online (Sandbox Code Playgroud)
帮我解决这个问题。我不知道即使在我之前的项目中已经使用了这个错误之后,我也不知道如何得到这个错误。
提前致谢
我在使用 Camera api 和 onActivityResult 的 Intent 时遇到了同样的问题,最后我发现它取决于您的 Android 设备版本,在 Android M (Marshmallow - 6.0\xe2\x80\x936.0.1 ) onActivityResult 意图如下所示:
\n\nrequestCode : 230 resultCode : -1 intent :Intent{dat=file:///storage/emulated/0/Pictures/Rahnama/IMG_20160508_121332_1519065 564.jpg typ=image/jpeg } RESULT_OK : -1\nRun Code Online (Sandbox Code Playgroud)\n\n对于低于 Marshmallow 的 Android 版本:
\n\nrequestCode : 230 resultCode : -1 intent : Intent { dat=content://media/external/images/media/309 (has extras) } RESULT_OK : -1\nRun Code Online (Sandbox Code Playgroud)\n\n我认为你必须检查 android 版本 onActivityResult 和 android 版本 Marshmallow 才能从 Uri 和 Intent 中的路径获取直接文件:
\n\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M){\n final Uri data = intent.getData();\n final File file = new File(data.getPath());\n // now you can upload your image file\n }else{\n // in android version lower than M your method must work\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我希望它对你有用。
\n| 归档时间: |
|
| 查看次数: |
19672 次 |
| 最近记录: |