android - 从 Uri 转换位图返回 null

标签 android image google-drive-api

<分区>

在我的 android 应用程序中,我使用 Google 驱动器来选择图像和文件,对于文件,我的代码大部分时间都运行良好。不幸的是,在某些情况下图像导入不起作用,图像位图返回空值,下面是我用来将内容 URI 转换为输入流和位图的代码。

Bitmap bitmap = null;
InputStream input = null;
try {
    input = getActivity()
        .getContentResolver()
        .openInputStream(
            Uri.parse("content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D83"));

    Log.Logger()
        .finest("Profileimagefile Fragment Drive called input" + input);

    bitmap = BitmapFactory
        .decodeStream(input);
} catch (FileNotFoundException e) {

    e.printStackTrace();
}

我提到了无效的 URI,请帮助我找出问题。

最佳答案

试试这个从以下位置获取位图:

Uri sArtworkUri = Uri
    .parse("content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D83");
Uri uris = ContentUris.withAppendedId(sArtworkUri,
    Long.parseLong(fileUri));

ContentResolver res = getContentResolver();
InputStream in = null;
try { in = res.openInputStream(uris);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Bitmap artwork = BitmapFactory.decodeStream( in );

使用这件艺术品....

关于android - 从 Uri 转换位图返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206483/

相关文章:

c++ - 如何在android项目中使用NDK?

java - SpongycaSTLe 提供程序无法在 Android 上运行

android - 如何在android XML中连接多个字符串?

ruby-on-rails - 在 Rails 3.1 on Production (Heroku) 中显示为空白的图像

c - PPM 将二进制转换为 ASCII - 无法解释的质量损失

android - 清除 Android 5+ 目录树选择器 (ACTION_OPEN_DOCUMENT_TREE) 中的历史记录(最近的文件夹)

c++ - 如何在 go 中将 RGB byte[] slice 转换为 image.Image?

java - 如何在不重复的情况下在谷歌驱动器中创建文件夹?

google-apps-script - Google 表格 - 当文件名相同时,脚本为 “Replace” 而不是 “Create”

curl - 如何使用 cURL 连接到 Google Drive API?