Skip to content

Instantly share code, notes, and snippets.

@alexzaitsev
Last active January 18, 2024 21:13
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexzaitsev/75c9b36dfcffd545c676 to your computer and use it in GitHub Desktop.
Save alexzaitsev/75c9b36dfcffd545c676 to your computer and use it in GitHub Desktop.
Get image/video file path from New Google Photos app Uri
if (isNewGooglePhotosUri(uri)) {
String pathUri = uri.getPath();
String newUri = pathUri.substring(pathUri.indexOf("content"), pathUri.lastIndexOf("/ACTUAL"));
return getDataColumn(context, Uri.parse(newUri), null, null);
}
public static boolean isNewGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@dlukashev
Copy link

this won't for cloud photos - uri will be like "content://com.google.android.apps.photos.contentprovider/0/1/https%3A%2F%2Flh3.googleusercontent.com%2xxxxx/ACTUAL"
and as a result you will get "content.com%2xxxxx" as new uri

It seems the only way is to copy content directly from getContentResolver().openInputStream(uri) in this case

@alexzaitsev
Copy link
Author

Seems u r right

@huapeek
Copy link

huapeek commented Aug 23, 2015

This won't work for photos from camera:
content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A%2FAF1QipMlPBXX945_K2FtImE860b5HQvY9SnY-igZycKO/ACTUAL/1597653450

@henriquedesousa
Copy link

Yep, doesn't work with photos from camera. Anyone has a fix ?

@benwilber
Copy link

I worked around this by consuming the input stream getContentResolver().openInputStream(uri) to a null output stream like:

import java.io.OutputStream ;

/** /dev/null */
public class NullOutputStream extends OutputStream {

    private static final NullOutputStream DEV_NULL = new NullOutputStream();

    private long mBytesWritten;

    public static OutputStream sink() {
        return new NullOutputStream();
    }

    public NullOutputStream() {
        mBytesWritten = 0;
    }

    @Override public void write(int b) {
        mBytesWritten += 1;
    }

    @Override public void write(byte b[]) {
        mBytesWritten += b.length;
    }

    @Override public void write(byte b[], int off, int len) {
        mBytesWritten += len;
    }

    public long getBytesWritten() {
        return mBytesWritten;
    }
}

using guava

NullOutputStream outputStream = new NullOutputStream();
ByteStreams.copy(inputStream, outputStream);
long size = outputStream.getBytesWritten();

it's not perfect but better than writing a tempfile.

@Mahan3340
Copy link

then how do u know if its image or video to save I to a file ? I mean the extension?

@anurags2102
Copy link

anurags2102 commented Nov 2, 2017

I am doing deeplinking in my app. So I've opened google photos and by tap on share icon, opening my app and sending image path. I am getting following path in intent:

content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/local%253A02ae48d5-e576-449b-994f-1db260da79c0/REQUIRE_ORIGINAL/NONE/2022132799

When I have tried your code its crashing and not getting actual path to download image. Please tell me what I am doing wrong.

@nieldeokar
Copy link

@anurags2102 : I was also receiving the path like this :

content://com.google.android.apps.photos.contentprovider/0/2/mediakey%3A%2Flocal%253A6a6204a4-2693-45f1-830f-dd7a8a945f00/REQUIRE_ORIGINAL/NONE/2116805895

In this case we get null path using above getDataColumn() method so I just checked for null there and used this solution for copying file.

@arindamxd
Copy link

@Parag2385
Copy link

@arindamxd your suggestion helped, was trying to solve it for a day, thanks :)

@arindamxd
Copy link

@Parag2385 welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment