Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maninp/464c6e39019446273d3122ad7f3f6e24 to your computer and use it in GitHub Desktop.
Save maninp/464c6e39019446273d3122ad7f3f6e24 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment