Search code examples
androidstringfileonactivityresult

Get path from gallery without failing delivering Result


My application works fine on some devices. Unfortunately others reported the following bug Failure delivering result which i dont know how to solve

So i use the following code:

 @Override
         public void onActivityResult(int requestCode, int resultCode, Intent data) {

                    if (requestCode == 1 && data!=null) {

                        if (resultCode == RESULT_OK) {

                        Uri uri = data.getData();

                        String s = getPath(uri); //Null string on some devices
                     if(s!=null){
                         File f = new File(s);
                         }
                        else{
                    //????????
                          }
                        if(resultCode== RESULT_CANCELED){

                        }

                }
            }

And one method called getPath:

public String getPath(Uri uri) {
    // just some safety built in 
    if( uri == null ) {

        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    return uri.getPath();

} Also when user click:

image = (ImageView) findViewById(R.id.imageView7);
    i.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent intent1 = new Intent();
             intent1.setType("image/*");
             intent1.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(Intent.createChooser(intent1,"Picture"), 1);
        }
    });

And lastly what i got from logcat

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content: flg=0x1 }} to activity {****************.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at *************.MainActivity.onActivityResult(Unknown Source)
at android.app.Activity.dispatchActivityResult(Activity.java:5535)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
... 11 more

What am i doing wrong?


Solution

  • Try this intent

    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
    

    Try this onActivityResult:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_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();
                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
        }