android selects pictures from local album uri Samsung mobile phone adaptation problem

Reprint address:

Start system album intent

Intent intentFromGallery = new Intent();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {//4.4 and above
} else {//Below 4.4
intentFromGallery.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);

Call back onActivityResult() after selecting any picture from the system album

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case IMAGE_REQUEST_CODE://Returned from album
                    Uri originalUri = data.getData();
                    try {
                     //To prevent memory overflow caused by too large original image, first reduce the original image display, and then release the memory occupied by original Bitmap
                        Bitmap photo = BitmapUtils.getAngleBitmap(this, originalUri);
                    } catch (Exception e) {
A normal cell phone gets a uri like this:

But the uri that Samsung mobile gets is like this:


Because of this, the content provider cannot be found, so an exception will occur:

Cursor cursor = cr.query(newUri, null, null, null, null);// Find from database according to Uri

The above method can not get the correct cursor, resulting in

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

Can't get the right value to get the right picture

The solution is as follows: reassemble the value of the uri once to convert it into a correct usable uri. The specific method is as follows / / the scheme copied from the stackoverflow is tested and passed

     * Adjust the angle of photos after taking photos or selecting photo albums.
     * @param mImageCaptureUri
     * @return Adjusted photos
    public static Bitmap getAngleBitmap(Context appContext, Uri mImageCaptureUri) {
        //Copied from stack overflow
        Uri newUri;
            String unusablePath = mImageCaptureUri.getPath();
            int startIndex = unusablePath.indexOf("external/");
            int endIndex = unusablePath.indexOf("/ACTUAL");
            String embeddedPath = unusablePath.substring(startIndex, endIndex);
            Uri.Builder builder = mImageCaptureUri.buildUpon();
            newUri =;
        }else {
            newUri = mImageCaptureUri;

        // No matter taking photos or selecting pictures, each picture is stored in the data and the corresponding rotation angle orientation value is also stored
        // So when we take out the picture, we take out the angle value so that we can display the picture correctly, without the effect of rotation
        ContentResolver cr = appContext.getApplicationContext().getContentResolver();
        Cursor cursor = cr.query(newUri, null, null, null, null);// Find from database according to Uri
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();// Move the cursor to the first place, because the Uri here contains ID, so it's the only one that doesn't need to loop to find the first one
            String filePath = cursor.getString(column_index);// Get picture path
            String orientation = cursor.getString(cursor
                    .getColumnIndex("orientation"));// Get the angle of rotation
            if (filePath != null) {
                Bitmap bitmap = getResizedImage(filePath, 500);// Read resource image according to Path
                int angle = 0;
                if (orientation != null && !"".equals(orientation)) {
                    angle = Integer.parseInt(orientation);
                if (angle != 0) {
                    // The main function of the following method is to turn the picture to an angle, or enlarge or shrink it
                    Matrix m = new Matrix();
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    m.setRotate(angle); // Rotation angle
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                            m, true);// Generate picture from New
                return bitmap;
        return null;

Tags: Android Database Mobile

Posted on Sun, 31 May 2020 08:05:05 -0700 by funkyfela