【问题标题】:New KitKat URIs dont respond to Intent.ACTION_VIEW新的 KitKat URI 不响应 Intent.ACTION_VIEW
【发布时间】:2014-02-19 16:06:46
【问题描述】:

由于 KitKat 已将 URI 从选择器更改为类似

 content://com.android.providers.media.documents/document/image:3951

然后我的 ACTION_VIEW 意图不再起作用。 例如,当用户选择图像时,我使用

public static void openImage(Fragment f, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/*");

        f.startActivity(intent);
    }

Android 图库和 Google+ 照片会出现,但选择后,图库只会显示空白屏幕,照片显示“未找到媒体”

声音也一样,我习惯用

public static void playSound(Fragment f, Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "audio/*");

    f.startActivity(intent);
}

在以前的版本中用于显示 Google Play 音乐,带有白色的小播放对话框 UI。使用新的 URI,我发现没有应用程序能够处理此意图。

// 对于照片,有趣的是,当您在新的 KK 选择器 UI 中选择图库而不是图片时,它会返回有效的旧 URI。

有什么想法吗? 系统应用程序是否还没有为新的 uri 做好准备?我是否应该以某种方式将新的 uri 破解为旧的 uri 以使意图起作用?还是我错过了什么?

谢谢!

【问题讨论】:

  • 由于新的 StorageProvider,Google 已经改变了这一点。我想你可以看看这里:s.stefma.ws/62ad44
  • 嗯,好吧,我明白他们为什么这样做了。但与此同时,他们打破了 ACTION_VIEW 的意图。我所看到的只是如何通过 inputStream 或 parcelFileDescriptor 导入,这在我的用例中是不想要的。我只想将 uris 存储在我的数据库中,并像以前一样在外部应用程序中查看它们。
  • 在这种情况下,接收应用程序似乎没有正确处理 uri,例如他们试图解析它们,而不是openInputStream()

标签: android android-intent uri


【解决方案1】:

解决方案是将标志 FLAG_GRANT_READ_URI_PERMISSION 传递给意图(在 KitKat 及更高版本上):

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

还要确保此内容 Uri 是从 ACTION_OPEN_DOCUMENT 意图中检索到的,如下所述:https://stackoverflow.com/a/19874645/334209

【讨论】:

    【解决方案2】:

    您可以使用以下意图从图库中选择图像

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

    然后在onActivityResult中获取选中的图片,

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
        if(data==null)return;
    
        try {
          Bitmap bit = scaleImage(this, data.getData());
          img.setImageBitmap(bit);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
            InputStream is = context.getContentResolver().openInputStream(photoUri);
    
            BitmapFactory.Options dbo = new BitmapFactory.Options();
    
            dbo.inJustDecodeBounds = true;
    
            BitmapFactory.decodeStream(is, null, dbo);
    
            is.close();
    
            int rotatedWidth, rotatedHeight;
            int orientation = getOrientation(context, photoUri);
    
            if (orientation == 90 || orientation == 270) {
                rotatedWidth = dbo.outHeight;
                rotatedHeight = dbo.outWidth;
            } else {
                rotatedWidth = dbo.outWidth;
                rotatedHeight = dbo.outHeight;
            }
    
            Bitmap srcBitmap;
            is = context.getContentResolver().openInputStream(photoUri);
            if (rotatedWidth > 100 || rotatedHeight > 100) {
                float widthRatio = ((float) rotatedWidth) / ((float) 100);
                float heightRatio = ((float) rotatedHeight) / ((float) 100);
                float maxRatio = Math.max(widthRatio, heightRatio);
    
                // Create the bitmap from file
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = (int) maxRatio;
                srcBitmap = BitmapFactory.decodeStream(is, null, options);
            } else {
                srcBitmap = BitmapFactory.decodeStream(is);
            }
            is.close();
    
            /*
             * if the orientation is not 0 (or -1, which means we don't know), we
             * have to do a rotation.
             */
            if (orientation > 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(orientation);
    
                srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                        srcBitmap.getHeight(), matrix, true);
            }
    
            String type = context.getContentResolver().getType(photoUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (type.equals("image/png")) {
                srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
                srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            }
            byte[] bMapArray = baos.toByteArray();
            baos.close();
            return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
        }
    
        public static int getOrientation(Context context, Uri photoUri) {
            /* it's on the external media. */
            Cursor cursor = context.getContentResolver().query(photoUri,
                    new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    
            if (cursor.getCount() != 1) {
                return -1;
            }
    
            cursor.moveToFirst();
            return cursor.getInt(0);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2014-03-30
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      相关资源
      最近更新 更多