Choosing photo using new Google Photos app is broken

后端 未结 3 1242
情深已故
情深已故 2020-11-27 12:23

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:



        
相关标签:
3条回答
  • 2020-11-27 12:59

    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();
    
    0 讨论(0)
  • 2020-11-27 13:04

    I know this question is bit old, still I did the following to get the image from gallery and use it. Below is the extension function for the same.

    fun Uri.toBitmap(context: Context?): Bitmap {
        return MediaStore.Images.Media.getBitmap(context?.contentResolver, this)
    }
    

    It takes context as parameter to get contentResolver and Uri received from onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?).

    To use, just do the following:

    intent?.data.toBitmap(context)
    

    where intent?.data is the Uri received from onActivityResult().

    0 讨论(0)
  • 2020-11-27 13:14

    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);
    }
    
    0 讨论(0)
提交回复
热议问题