Android Gallery on Android 4.4 (KitKat) returns different URI for Intent.ACTION_GET_CONTENT

后端 未结 19 2310
温柔的废话
温柔的废话 2020-11-22 02:44

Before KitKat (or before the new Gallery) the Intent.ACTION_GET_CONTENT returned a URI like this

content://media/external/images/media/39

19条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 03:30

    Just as Commonsware mentioned, you shouldn't assume, that the stream you get via ContentResolver is convertable into file.

    What you really should do is to open the InputStream from the ContentProvider, then create a Bitmap out of it. And it works on 4.4 and earlier versions as well, no need for reflection.

        //cxt -> current context
    
        InputStream input;
        Bitmap bmp;
        try {
            input = cxt.getContentResolver().openInputStream(fileUri);
            bmp = BitmapFactory.decodeStream(input);
        } catch (FileNotFoundException e1) {
    
        }
    

    Of course if you handle big images, you should load them with appropriate inSampleSize: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html. But that's another topic.

提交回复
热议问题