Search code examples
androidandroid-intentintentfiltermediastore

RuntimeException on getting image from another applications


I've set one of my activity to receive image from another applications:

<activity
        android:name=".activities.GetShareImage"
        android:screenOrientation="portrait">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>

and use that image in my app. But I got this exception some times:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{********.activities.GetShareImage}: java.lang.IllegalArgumentException: column '_data' does not exist

my code is this:

Intent intent = getIntent();
    if (intent != null){

        String action   = intent.getAction();
        String type     = intent.getType();

        if(Intent.ACTION_SEND.equals(action) && type != null){

            if(type.startsWith("image/")){

                Bundle extra = intent.getExtras();
                if(extra.containsKey(Intent.EXTRA_STREAM)){

                    Uri imageUri    = extra.getParcelable(Intent.EXTRA_STREAM);


                    if (imageUri != null){

                        String Scheme   = imageUri.getScheme();

                        if(Scheme.equals("content")){


                            ContentResolver contentResolver = getContentResolver();
                            Cursor cursor = contentResolver.query(imageUri, null, null, null, null);
                            cursor.moveToFirst();

                            String ImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                            if (ImagePath != null) {
                                Uri imageURI = Uri.fromFile(new File(ImagePath));
                                startCropActivity(imageURI);
                            }else {


                                finish();
                            }

                        } else if (Scheme.equals("file")){

                            Uri imgUri  = Uri.parse(extra.getParcelable(Intent.EXTRA_STREAM).toString());
                            startCropActivity(imgUri);
                        }

                    } else {
                        finish();
                    }

                }

            }

        }
    }

I've check the Uri scheme to prevent this issue but it seems it's not enough. Then what is the best way to get image from other apps? Is there any better solution to do that?


Solution

  • Neither your content nor your file code is correct. In the case of content, you assume that the MediaStore knows about the image and has a path that you can use, neither of which are necessarily correct. In the case of file, you get a Uri, convert it to a String, then convert it back to a Uri, for no obvious reason.

    In both cases, extra.getParcelable(Intent.EXTRA_STREAM) is the Uri. There is no need for you do to anything with that, other than pass it to startCropActivity(), whatever that does.