【问题标题】:Download image from new Google+ (plus) Photos Application从新的 Google+(加号)照片应用程序下载图片
【发布时间】:2013-11-06 07:01:54
【问题描述】:

最近,Google 为 Google+(加号)添加了照片应用,当您启动 Intent 以选择图片时,它就会显示出来。但是,如果我从 Google+ 照片中选择图像并尝试在我的应用程序中使用它,我当前的逻辑都无法返回可用的 URI 或 URL 来实际获取我可以下载和操作的图像。我目前正在使用“通用”方法来尝试操作可以在 Stack Overflow 和其他地方找到的 URI。如果需要,我可以提供代码,但在这一点上,我认为这有点无关紧要,因为它适用于除了这个新应用程序之外的所有其他东西。关于如何获得可用图像的任何想法?

URI 如下所示:

content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%<a bunch of letters and numbers here>

无论我从 Google 照片应用中选择什么,MediaColumns.DATA 信息始终返回 null,MediaColumns.DISPLAY_NAME 始终返回 image.jpg。如果我尝试在浏览器中粘贴从 https 到末尾的所有内容,则什么都不会出现。不知道如何从中获取有用的信息。

【问题讨论】:

  • 我也有同样的问题。我需要文件内容以及文件名/标题,但我得到的只是null ...

标签: android google-plus android-contentprovider google-photos


【解决方案1】:

当接收到数据意图时,你应该使用 contentResolver 来获取照片。 这是你应该做的:

String url = intent.getData().toString();
Bitmap bitmap = null;
InputStream is = null;
if (url.startsWith("content://com.google.android.apps.photos.content")){
       is = getContentResolver().openInputStream(Uri.parse(url));
       bitmap = BitmapFactory.decodeStream(is);
}

【讨论】:

  • Duh...当它识别图像时,我有逻辑可以捕捉到这一点,但我忘记在我的 AsyncTask 中添加一个 if 语句来处理这个问题,该语句实际上将图像拉下并创建了一个文件。一旦我这样做了,它现在就抓取了图像。
  • 你设法让它工作了吗?对于图像,获取位图并将其保存到文件中是可行的。在这种情况下,您将如何处理视频?
  • 有没有办法使用这种方法提取图像名称和其他元数据?
  • 对于某些图像,is 为空。有人知道为什么吗?
【解决方案2】:

我确实遇到了从新的 Google 相册应用中选择图片的问题。我能够通过以下代码解决它。

它对我有用,基本上我所做的是检查内容 URI 中是否存在任何 权限。如果它在那里,我正在写入临时文件并返回该临时图像的路径。您可以在写入临时图像时跳过压缩部分

public static String getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;
    if (uri.getAuthority() != null) {
        try {
            is = context.getContentResolver().openInputStream(uri);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            return writeToTempImageAndGetPathUri(context, bmp).toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

附: : 我已经回复了similar question here

【讨论】:

    【解决方案3】:

    您必须使用投影才能获得ImageColumns.DATA(或MediaColumns.DATA):

    private String getRealPathFromURI(Uri contentURI) {
        // Projection makes ContentResolver to get needed columns only 
        String[] medData = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(contentURI, medData, null, null, null);
    
        // this is how you can simply get Bitmap
        Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), contentURI);
    
        // After using projection cursor will have needed DATA column
        cursor.moveToFirst();
        final int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    
        return cursor.getString(idx);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多