我有一个图库照片选择工具,但是每次选择它时,我的程序都会出错。我无法纠正错误,我尝试了许多不同的方法,但是我没有工作。我正在等待帮助。
下面是我用来从图库中选择照片的功能。
private void openPickImageIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Bir Görsel Seçin"), PICK_IMAGE);
}
这是我的onResult函数。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().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();
String token = SatekApplication.getInstance().getLoginResponseDto().getToken();
File file = new File(filePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.create(reqFile);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), photoFileName + ".jpg");//todo test
Call<ResponseBody> req = service.uploadImage(token, asset.getUploadUrl(), reqFile);
EventBus.getDefault().post(new ShowLoadingDialogEvent());
photoUploadingProgressBar.setVisibility(View.VISIBLE);
buttonUploadImage.setEnabled(false);
imageViewTick.setVisibility(View.GONE);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
EventBus.getDefault().post(new HideLoadingDialogEvent());
createAsset();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
EventBus.getDefault().post(new HideLoadingDialogEvent());
photoUploadingProgressBar.setVisibility(View.GONE);
buttonUploadImage.setEnabled(true);
t.printStackTrace();
}
});
}
}
这是我收到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.satisekibim.android, PID: 7241
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65637, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:61 flg=0x1 }} to activity {com.satisekibim.android/com.satisekibim.android.products.product.addproduct.AddProductActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:282)
at com.satisekibim.android.products.product.addproduct.AddProductFragment.onActivityResult(AddProductFragment.java:518)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)