错误:无法提供结果ResultInfo {who = null,request = 100, result = -1,data = Intent { DAT =含量://com.android.providers.media.documents/document/image:28362 flg = 0x1}}到活动 {com.projectbox.uploadfile / com.projectbox.uploadfile.MainActivity}: java.lang.NullPo
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
android.net.Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null)
return;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
File file = new File(filePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("banner", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "banner");
Log.d("THIS", data.getData().getPath());
retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("FFF", t.getMessage());
t.printStackTrace();
}
});
}
}
错误堆栈:
Process: com.projectbox.uploadfile, PID: 17822
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:28362 flg=0x1 }} to activity {com.projectbox.uploadfile/com.projectbox.uploadfile.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4220)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4263)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:262)
at com.projectbox.uploadfile.MainActivity.onActivityResult(MainActivity.java:110)
at android.app.Activity.dispatchActivityResult(Activity.java:7025)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4216)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4263)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
答案 0 :(得分:0)
似乎filepath为null。您正在尝试使用mediastore DATA
列从content://
URI获取文件。这可能会或可能不会像Mark Murphy在How to Consume Content From a Uri中所解释的那样发挥作用。
获取内容的唯一可靠方法是使用ContentResolver.openInputStream()
然后,您可以在上传之前将流复制到文件中,也可以按照此OkHttp recipe的说明直接上传流。
答案 1 :(得分:0)
您在此处获得错误的图像路径或相对图像路径。我认为一定是问题所在..
String[] filePathColumn = {MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null)
return;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Cursor cursor = null;
Uri contentUri = data.getData();
String filepath = "";
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
希望这能解决你的问题。
答案 2 :(得分:0)
尝试启用开发人员选项中的活动...这可能是我之前遇到的原因之一
答案 3 :(得分:0)
if(!cursor.moveToFirst())
或if(TextUtils.isEmpty(filePath))
您应该从方法返回:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
android.net.Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null)
return;
if(!cursor.moveToFirst()){
cursor.close();
return;
}
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if(TextUtils.isEmpty(filePath))
return;
File file = new File(filePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("banner", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "banner");
Log.d("THIS", data.getData().getPath());
retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("FFF", t.getMessage());
t.printStackTrace();
}
});
}
}
答案 4 :(得分:-1)
你可以这样做。
if (requestCode == 3 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// picturePath = cursor.getString(columnIndex);
// try to copy image 1
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
picturePath = "" + destination;
cursor.close();
img_member.setTag(picturePath);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
img_member.setImageBitmap(bitmap);
mBottomSheetDialog.hide();
} catch (IOException e) {
e.printStackTrace();
}
}