【问题标题】:RuntimeException on getting image from another applications从其他应用程序获取图像时出现 RuntimeException
【发布时间】:2017-02-02 22:47:32
【问题描述】:

我已将我的一项活动设置为从其他应用程序接收图像:

<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>

并在我的应用程序中使用该图像。但我有时会遇到这个异常:

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

我的代码是这样的:

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

                }

            }

        }
    }

我已经检查了Uri 方案来防止这个问题,但似乎这还不够。那么从其他应用程序获取图像的最佳方法是什么? 有没有更好的解决方案?

【问题讨论】:

  • 为什么你认为imageUri 指向媒体存储?为什么你认为它根本指向某个文件?

标签: android android-intent intentfilter mediastore


【解决方案1】:

您的contentfile 代码都不正确。在content 的情况下,您假设MediaStore 知道图像并且具有您可以使用的路径,但两者都不一定正确。对于file,你得到一个Uri,将其转换为String,然后将其转换回Uri,没有明显的原因。

在这两种情况下,extra.getParcelable(Intent.EXTRA_STREAM) Uri。除了将它传递给 startCropActivity() 之外,您无需对此做任何事情。

【讨论】:

  • 我需要那个字符串还有一个原因,我已经从代码中删除了,因此它看起来很傻:)
猜你喜欢
  • 2019-04-11
  • 1970-01-01
  • 2023-03-27
  • 2014-02-03
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
相关资源
最近更新 更多