Download image from new Google+ (plus) Photos Application

风流意气都作罢 提交于 2019-11-28 08:03:39
HatemTmi

When receiving the data intent, you should use the contentResolver to get the photos. Here's what you should do:

String url = intent.getData().toString();
Bitmap bitmap = null;
InputStream is = null;
if (url.startsWith("content://com.google.android.apps.photos.content")){
       is = getContentResolver().openInputStream(Uri.parse(url));
       bitmap = BitmapFactory.decodeStream(is);
}
Akhil

I did faced issues selecting images from new Google Photos app. I was able to resolve it by below code.

It works for me, basically what i did is i am checking if there is any authority is there or not in content URI. If it is there i am writing to temporary file and returning path of that temporary image. You can skip compression part while writing to temporary image

public 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 {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public 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);
}

P.S. : I have answered a similar question here

You have to use projection in order to get ImageColumns.DATA (or MediaColumns.DATA):

private String getRealPathFromURI(Uri contentURI) {
    // Projection makes ContentResolver to get needed columns only 
    String[] medData = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentURI, medData, null, null, null);

    // this is how you can simply get Bitmap
    Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), contentURI);

    // After using projection cursor will have needed DATA column
    cursor.moveToFirst();
    final int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 

    return cursor.getString(idx);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!