Getting an image from Gallery on from the Picasa//Google + synced folders doesn't work Getting an image from Gallery on from the Picasa//Google + synced folders doesn't work android android

Getting an image from Gallery on from the Picasa//Google + synced folders doesn't work


I wasted now lots of hours and now i found a solution which works in all cases without any magic downloading in special threads or something. The following method returns a stream from the content which the user selected and this works with everything in the wild.

FileInputStream getSourceStream(Uri u) throws FileNotFoundException {    FileInputStream out = null;    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        ParcelFileDescriptor parcelFileDescriptor =                mContext.getContentResolver().openFileDescriptor(u, "r");        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();        out = new FileInputStream(fileDescriptor);    } else {        out = (FileInputStream) mContext.getContentResolver().openInputStream(u);    }    return out;}


After a lot of research, I felt that there were too many workarounds to do something which seems to be quite simple. So, I went ahead a wrote a small library that handles all the complex if elses and happens to work for all possible scenarios that I can think of. Do give it a try and see if this helps.

http://techdroid.kbeanie.com/2013/03/easy-image-chooser-library-for-android.html

This is an initial implementation. Although it handles almost every situation, there could be a few bugs. If you use this library, please let me know your feedback and if at all it could be improved any further.


I faced the same problem about year ago.. I show you my solution (code is quite messy, please refactor it).So, you have the image URI which was returned from gallery:

    ImageInfo getImage(URI imageUri) {        ImageInfo result = null;        final String[] cursorColumns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};        // some devices (OS versions return an URI of com.android instead of com.google.android        if (imageUri.toString().startsWith("content://com.android.gallery3d.provider"))  {            // use the com.google provider, not the com.android provider.            imageUri = Uri.parse(imageUri.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));        }        Cursor cursor = App.getContext().getContentResolver().query(imageUri, cursorColumns, null, null, null);        if (cursor != null) {            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);            int orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);            cursor.moveToFirst();            // if it is a picasa image on newer devices with OS 3.0 and up            if (imageUri.toString().startsWith("content://com.google.android.gallery3d")) {                result = new ImageInfo(downloadImage(imageUri), "0");                                   } else { // it is a regular local image file                result = new ImageInfo(cursor.getString(dataColumnIndex), cursor.getString(orientationColumnIndex));            }            cursor.close();        } else {            result = new ImageInfo(downloadImage(imageUri), "0");        }        return result;                      }

And now we need function to download image:

private String downloadImage(URI imageUri) {    File cacheDir;    // if the device has an SD card    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {        cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),".OCFL311");    } else {        // it does not have an SD card        cacheDir = App.getContext().getCacheDir();    }    if(!cacheDir.exists()) cacheDir.mkdirs();    File f = new File(cacheDir, PUT_HERE_FILE_NAME_TO_STORE_IMAGE);    try {        InputStream is = null;        if (imageUri.toString().startsWith("content://com.google.android.gallery3d")) {            is = App.getContext().getContentResolver().openInputStream(imageUri);        } else {            is = new URL(imageUri.toString()).openStream();        }        OutputStream os = new FileOutputStream(f);        Utils.InputToOutputStream(is, os);        return f.getAbsolutePath();    } catch (Exception ex) {        Log.d(this.getClass().getName(), "Exception: " + ex.getMessage());        // something went wrong        ex.printStackTrace();        return null;    }}

ImageInfo is my class to store path to image and its orientation.

public static class ImageInfo {    public final String filePath;    public final String imageOrientation;    public ImageInfo(String filePath, String imageOrientation) {        this.filePath = filePath;        if (imageOrientation == null) imageOrientation = "0";        this.imageOrientation = imageOrientation;               }}