Created
September 25, 2015 07:29
-
-
Save SeongUgJung/b93a1cbd889b672e8b06 to your computer and use it in GitHub Desktop.
Android Google Photos's Uri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Anyone has import class or source for GoogleImagePickerUtil?
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.
Replace to:
File extDir = context.getExternalFilesDir(null);
filePath = extDir.getAbsolutePath() + "/IMG_" + UUID.randomUUID().toString() + ".jpg";
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
What is that GoogleImagePickerUtil thing?