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

后端 未结 19 2266
温柔的废话
温柔的废话 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:22

    This worked fine for me:

    else if(requestCode == GALLERY_ACTIVITY_NEW && resultCode == Activity.RESULT_OK)
    {
        Uri uri = data.getData();
        Log.i(TAG, "old uri =  " + uri);
        dumpImageMetaData(uri);
    
        try {
            ParcelFileDescriptor parcelFileDescriptor =
                    getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            Log.i(TAG, "File descriptor " + fileDescriptor.toString());
    
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
    
            options.inSampleSize =
               BitmapHelper.calculateInSampleSize(options,
                                                  User.PICTURE_MAX_WIDTH_IN_PIXELS,
                                                  User.PICTURE_MAX_HEIGHT_IN_PIXELS);
            options.inJustDecodeBounds = false;
    
            Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
            imageViewPic.setImageBitmap(bitmap);
    
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            // get byte array here
            byte[] picData = stream.toByteArray();
            ParseFile picFile = new ParseFile(picData);
            user.setProfilePicture(picFile);
        }
        catch(FileNotFoundException exc)
        {
            Log.i(TAG, "File not found: " + exc.toString());
        }
    }
    

提交回复
热议问题