Download image from new Google+ (plus) Photos Application
Asked Answered
T

3

31

Recently Google added the Photos app for Google+ (plus) and it shows up when you launch an Intent to choose an image. However, if I select an image from Google+ Photos and try to use it in my application none of my current logic is able to return a usable URI or URL to actually get an image that I can download and manipulate. I'm currently using the "common" methods to try to manipulate the URI that can be found here on Stack Overflow and elsewhere. I can provide code if needed, but at this point I think it's kind of irrelevant since it works well for everything else except this new app. Any ideas on how to get a usable image?

The URI looks something like the following:

content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%<a bunch of letters and numbers here>

The MediaColumns.DATA info always returns null and the MediaColumns.DISPLAY_NAME always returns image.jpg no matter what I select from the Google Photos app. If I try to paste everything from https to the end in my browser, nothing comes up. Not sure how to get usable info from this.

Tipton answered 6/11, 2013 at 7:1 Comment(1)
I have the same issue. I need the file contents as well as a file name/title but all I get is null ...Fujio
W
32

When receiving the data intent, you should use the contentResolver to get the photos. Here's what you should do:

String url = intent.getData().toString();
Bitmap bitmap = null;
InputStream is = null;
if (url.startsWith("content://com.google.android.apps.photos.content")){
       is = getContentResolver().openInputStream(Uri.parse(url));
       bitmap = BitmapFactory.decodeStream(is);
}
Wellfixed answered 7/11, 2013 at 12:11 Comment(4)
Duh...I had logic to catch this when it identified the image but I forgot to add an if statement to handle this in my AsyncTask that actually pulled the image down and created a file. Once I did that it now grabbed the image.Tipton
Did you manage to get it working? For images, getting a bitmap and saving it to a file would work. How would you handle a video in such a case?Absentminded
Is there a way to extract the image name and other metadata using this method?Primm
For some images is is null. Does somebody know why?Uncommon
G
1

I did faced issues selecting images from new Google Photos app. I was able to resolve it by below code.

It works for me, basically what i did is i am checking if there is any authority is there or not in content URI. If it is there i am writing to temporary file and returning path of that temporary image. You can skip compression part while writing to temporary image

public static String getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;
    if (uri.getAuthority() != null) {
        try {
            is = context.getContentResolver().openInputStream(uri);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            return writeToTempImageAndGetPathUri(context, bmp).toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

P.S. : I have answered a similar question here

Groggery answered 24/6, 2015 at 7:7 Comment(0)
Y
0

You have to use projection in order to get ImageColumns.DATA (or MediaColumns.DATA):

private String getRealPathFromURI(Uri contentURI) {
    // Projection makes ContentResolver to get needed columns only 
    String[] medData = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentURI, medData, null, null, null);

    // this is how you can simply get Bitmap
    Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), contentURI);

    // After using projection cursor will have needed DATA column
    cursor.moveToFirst();
    final int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 

    return cursor.getString(idx);
}
Yardley answered 2/10, 2015 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.