使用新的存储访问框架作为图像的内容选择器,如何将生成的文件作为位图获取?如果内容是手机本地的,这很容易完成,如下面的代码所示。但是,如果内容来自诸如 picasa 或谷歌驱动器或盒子之类的地方,则内容不可访问,因为 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++;
}
}
}
}