android - Picasa 的存储访问框架

标签 android storage-access-framework

使用新的存储访问框架作为图像的内容选择器,如何将结果文件作为位图获取?如果内容是手机本地的,这很容易完成,如下面的代码所示。但是,如果内容来自 picasa 或 google drive 或 box 等地方,则内容不可访问,因为 BitmapFactory.decodeStream(InputStream) 始终返回 false。有解决办法吗?

// launch the new UI picker
Intent docsIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
docsIntent.addCategory(Intent.CATEGORY_OPENABLE);
docsIntent.setType("image/*");
startActivityForResult(docsIntent, 556);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = data.getData();
    // removed threading logic for easy of reading
    ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fd = pfd.getFileDescriptor();
    Bitmap bm = BitmapFactory.decodeFileDescriptor(fd); // null for picasa
    pfd.close();

    InputStream is = getContentResolver().openInputStream(uri);
    Bitmap bm2 = BitmapFactory.decodeStream(is); // null for picasa

     // nothing in the cursor that would point to a url to get the document. 
     Cursor c = getContentResolver().query(uri, null, null, null, null);
     if (c != null) {
        String[] names = c.getColumnNames();
        while (c.moveToNext()) {
            int columnCount = c.getColumnCount();
            int i =0;
            while (i<columnCount) {
                String value = c.getString(i);
                String columnName = c.getColumnName(i);
                Log.d("Junk", columnName + " : " + value);
                i++;
            }
        }
    }

}

最佳答案

Kumar Bibek 编写了一个非常好的 ImageChooser 库来管理大多数场景。我确信它可以处理 Picasa。

您可以在以下位置找到他的项目: https://github.com/coomar2841/image-chooser-library

关于android - Picasa 的存储访问框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689471/

相关文章:

android - 在 Android 10 中使用存储访问框架输出 URI 文件比 DocumentFile 更好的方法是什么?

java - CardView 未使用 LayoutInflater 正确膨胀

android - 使用 gradle 从 android 测试到 Sonar 的代码覆盖率

Android 存储访问框架和媒体扫描器

android - 探索从 Intent.ACTION_OPEN_DOCUMENT 检索到的 Uri 文件夹的内容

android - 使用存储访问框架创建文件

android - Mapbox Android 自定义导航 View (Navigation SDK)

带有 ScrollView 的 Android Studio ConstraintLayout

java - 我可以将可绘制对象的 int ID 存储在 XML 中吗?

android - 使用 Android 存储访问框架重命名 Google Drive 文档导致权限错误