Monday, April 30, 2018

OnActivityResult activity error : Failure delivering result ResultInfo

Leave a Comment

error : 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.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();             }         });     } } 

Error stack:

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)                                                                 

5 Answers

Answers 1

You can do it something like this.

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();             }          } 

Answers 2

It seems that the filepath is null. You are trying use the mediastore DATA column to get a File from a content:// URI. This may or may not work as explained by Mark Murphy in How to Consume Content From a Uri.

The only reliable way to get the content is to use ContentResolver.openInputStream()

You can then either copy the stream to a file before uploading it or directly upload the stream as explained by this OkHttp recipe.

Answers 3

You are getting wrong image path or Relative Image Path here. I Think that must Be the problem..

Try Replacing

        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(); 

With

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();             }         } 

Hope this will Solve your problem.

Answers 4

Try enabling keep activities from developer options ...that may be one of the cause I have faced earlier

Answers 5

if(!cursor.moveToFirst()) or if(TextUtils.isEmpty(filePath)) you should return from method:

@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();             }         });     } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment