Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Created September 25, 2015 07:29
Show Gist options
  • Save SeongUgJung/b93a1cbd889b672e8b06 to your computer and use it in GitHub Desktop.
Save SeongUgJung/b93a1cbd889b672e8b06 to your computer and use it in GitHub Desktop.
Android Google Photos's Uri
public String getImagePath(Context context, Uri uri){
if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGoogleOldPhotosUri(uri)) {
// return http path, then download file.
return uri.getLastPathSegment();
} else if (isGoogleNewPhotosUri(uri)) {
// copy from uri. context.getContentResolver().openInputStream(uri);
return copyFile(context, uri);
} else if (isPicasaPhotoUri(uri)) {
// copy from uri. context.getContentResolver().openInputStream(uri);
return copyFile(context, uri);
}
}
}
public static boolean isGoogleOldPhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public static boolean isGoogleNewPhotosUri(Uri uri) {
return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
}
private static boolean isPicasaPhotoUri(Uri uri) {
return uri != null
&& !TextUtils.isEmpty(uri.getAuthority())
&& (uri.getAuthority().startsWith("com.android.gallery3d")
|| uri.getAuthority().startsWith("com.google.android.gallery3d"));
}
private static String copyFile(Context context, Uri uri) {
String filePath;
InputStream inputStream = null;
BufferedOutputStream outStream = null;
try {
inputStream = context.getContentResolver().openInputStream(uri);
filePath = GoogleImagePickerUtil.getDownloadPath() + "/" + GoogleImagePickerUtil
.getWebImageName() + ".jpg";
outStream = new BufferedOutputStream(new FileOutputStream
(filePath));
byte[] buf = new byte[2048];
int len;
while ((len = inputStream.read(buf)) > 0) {
outStream.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
filePath = "";
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return filePath;
}
@Sloy
Copy link

Sloy commented Dec 20, 2016

What is that GoogleImagePickerUtil thing?

@Dgep
Copy link

Dgep commented Apr 17, 2017

Anyone has import class or source for GoogleImagePickerUtil?

@maninp
Copy link

maninp commented May 23, 2017

Wow!!!. Finally got the solution. It's working. I don't know about GoogleImagePickerUtil class. Instead, I defined the method to return the external path.

@tim4dev
Copy link

tim4dev commented Jun 27, 2017

Replace to:

File extDir = context.getExternalFilesDir(null);
filePath = extDir.getAbsolutePath() + "/IMG_" + UUID.randomUUID().toString() + ".jpg";

@naveen-aluri
Copy link

Replace to:

File extDir = context.getExternalFilesDir(null);
filePath = extDir.getAbsolutePath() + "/IMG_" + UUID.randomUUID().toString() + ".jpg";

Thanks a lot, it's working great!

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