Question

Using the new Storage Access Framework as a content picker for images, how do I get the resulting files as a bitmap? If the content is local to the phone this is easily done as shown in the code below. However, if the content comes from a place such as picasa or google drive or box, the content is not accessible as BitmapFactory.decodeStream(InputStream) always returns false. Are there solutions?

// 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++;
            }
        }
    }

}
Était-ce utile?

La solution 2

RESOLVED: Well it turns out all of the above code works just fine. When coming from the new UI picker on the Recent category, it's showing out of date images it seems which do not exist. This would explain why the image will not load.

Autres conseils

Kumar Bibek has written a very nice ImageChooser library that manages most scenarios. I know for sure it handles Picasa.

You can find his project at: https://github.com/coomar2841/image-chooser-library

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top