Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing photo using new Google Photos app is broken

My app has ability to select photo from library. Exactly I want file path from this selection.

This is the code to create intent for selecting photo:

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,         MediaStore.Images.Media.EXTERNAL_CONTENT_URI);     photoPickerIntent.setType("image/*");     startActivityForResult(photoPickerIntent, INTENT_REQUEST_CODE_SELECT_PHOTO); 

This is the code that gets file path from URI:

    Cursor cursor = null;     String path = null;     try {         String[] projection = { MediaStore.Images.Media.DATA };         cursor = context.getContentResolver().query(contentUri, projection, null, null, null);         int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);         cursor.moveToFirst();         path = cursor.getString(columnIndex);     } finally {         if (cursor != null) {             cursor.close();         }     }     return path; 

Before yesterday's update of Google Photos app everything worked perfectly fine. Now path is null after parsing URI.

URI is similar to this: content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL

I also tried to create intent with Intent.ACTION_GET_CONTENT action - no luck.

like image 840
Den Drobiazko Avatar asked May 29 '15 10:05

Den Drobiazko


People also ask

Why is my Google Photos app not working?

For Android: Go to Settings > Apps > Photos > Storage > CLEAR CACHE. Then, start your Google Photos to see if it works. For iPhone: Go to Settings > General > iPhone Storage > Google Photos> Delete App. This option will delete all Google Photos app data and cache.

Why is Google changing Google Photos?

Six months ago, we announced a change to our High quality storage policy that allows us to keep pace with the growing demand for storage and build Google Photos for the future.

What happened to Google Photos?

Starting July 10, 2019, Google Photos and Google Drive will no longer automatically sync. We're making this change to simplify how things work between the two services. You can read more about the changes in our blog post.


2 Answers

Below code is working for me to get content URI on latest Google Photos as well. What i have tried is writing to temp file and return temp image URI, if it has authority in content URI.

You can try same:

private 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             {                 if (is != null)                 {                     is.close();                 }             }             catch (IOException e)             {                 e.printStackTrace();             }         }     }     return null; }  private 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); } 
like image 137
Akhil Avatar answered Oct 09 '22 02:10

Akhil


This is most certainly a workaround, but you could extract the real content URI which has apparently become embedded for some reason: content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209

I was able to create a new URI with authority=media and path=external/images/media/xxx, and content resolver returned a real URL.

Example code:

String unusablePath = contentUri.getPath(); int startIndex = unusablePath.indexOf("external/"); int endIndex = unusablePath.indexOf("/ACTUAL"); String embeddedPath = unusablePath.substring(startIndex, endIndex);  Uri.Builder builder = contentUri.buildUpon(); builder.path(embeddedPath); builder.authority("media"); Uri newUri = builder.build(); 
like image 32
Jon Rogstad Avatar answered Oct 09 '22 02:10

Jon Rogstad