0

I'm selecting an image to use as User's picture in my app like this:

Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, IMAGE_GALLERY);

In onActivityResult()

if(requestCode == IMAGE_GALLERY && resultCode == RESULT_OK) {
    Uri uri = intent.getData();
    if(uri != null) {
        this.picture = Utils.ScaleBitmap(context, uri, 640);
        userPic.setScaleType(ImageView.ScaleType.CENTER_CROP);
        userPic.setPadding(0,0,0,0);
        userPic.setImageBitmap(picture);
    }
}

Where my Utils.ScaleBitmap method is the following:

try {
        //Getting file path from URI
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(imageURI, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);

        //Getting EXIF info to rotate image
        ExifInterface exif = null;
        try {
            File pictureFile = new File(picturePath);
            exif = new ExifInterface(pictureFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = ExifInterface.ORIENTATION_NORMAL;

        if (exif != null)
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                bitmap = rotateBitmap(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                bitmap = rotateBitmap(bitmap, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                bitmap = rotateBitmap(bitmap, 270);
                break;
        }

        //Compressing image
        int w = bitmap.getWidth(), h = bitmap.getHeight();
        int width, height;
        if (w > max || h > max) {
            if (w == h) {
                width = height = max;
            } else if (w < h) {
                height = max;
                width = max * w / h;
            } else {
                width = max;
                height = max * h / w;
            }
        } else {
            width = w;
            height = h;
        }
        //Bitmap bitmap = ((BitmapDrawable) commentImage.getDrawable()).getBitmap();
        Bitmap scaledphoto = Bitmap.createScaledBitmap(bitmap, width, height, true);
        return scaledphoto;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

The problem is that, this code doesn't work with images that I pick from the cloud, like Google Drive, Picasa, etc.

It used to work when I didn't do all the rotating stuff. It was only

Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageURI);

And I could pick any image and worked. But the images from my camera were getting a wrong orientation. Now I corrected the rotation, but cannot get images from cloud.

Does anyone know how could I get both things working?

I mean, I want to be able to select images from Cloud Storage, and also be able to rotate the images that have wrong orientation.

Gustavo Conde
  • 927
  • 12
  • 20
  • "this code doesn't work with images that I pick from the cloud, like Google Drive, Picasa, etc." -- there does not appear to be anything in this code that relates to the cloud. You are picking images from the `MediaStore`, which will be local. How are you picking images from the cloud? – CommonsWare Aug 24 '16 at 20:00
  • When I open the gallery a Picasa folder appears. And if I perform the intent with Photos app. I can select my photos.google.com pictures. So, ACTION_PICK is not only for Local pictures. – Gustavo Conde Aug 24 '16 at 20:32
  • `File pictureFile = new File(picturePath)` -- this only works for a `Uri` with a `file` scheme. Perhaps you are getting a different schemes, such as `content`. Developers who use libraries like Picasso or Glide get the image rotation, scaling, and all that as part of the library. If you do not want to use such a library, use [an `ExifInterface` that works with streams, rather than files](https://commonsware.com/blog/2016/05/31/tale-two-exifinterfaces.html). – CommonsWare Aug 24 '16 at 20:37
  • @CommonsWare: I'm ussing picasso, but not in this case because it doesn't work with bitmap, and I need to compress the image before using it. Am I complicating things to much? Should I not use a Bitmap? – Gustavo Conde Aug 24 '16 at 20:50
  • "I need to compress the image before using it" -- you are not compressing the image. You are scaling the image, and Picasso can do that for you, last I checked. See the `resize()` method. – CommonsWare Aug 24 '16 at 21:04
  • The thing is that I upload that image to a webserver. So I can show it with Picasso, but then I need the actual file to call the web service. – Gustavo Conde Aug 25 '16 at 14:30
  • "then I need the actual file to call the web service" -- there is no file. Get a better HTTP client library, one that supports an `InputStream` or a `Uri` for your upload operation. Otherwise, you will need to make a local copy of the image (read bytes in from the `InputStream` you get from `openInputStream()` on a `ContentResolver`, write them to a `FileOutputStream` to some file that you control). – CommonsWare Aug 25 '16 at 14:34

1 Answers1

0

Your issue is with getting images from cloud. This has been answered before. For an example look here

I still fetch bitmaps with

bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);

I needed the EXIF data as well, and below is my code based on @paulburke code

    /**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @author paulburke
 */
public static String getPathFromURI(final Context context, final Uri uri) {

    if (BuildConfig.DEBUG)
        Log.d(TAG, "Authority: " + uri.getAuthority() +
                       ", Fragment: " + uri.getFragment() +
                       ", Port: " + uri.getPort() +
                       ", Query: " + uri.getQuery() +
                       ", Scheme: " + uri.getScheme() +
                       ", Host: " + uri.getHost() +
                       ", Segments: " + uri.getPathSegments().toString());

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        } else if (isDownloadsDocument(uri)) {
            // DownloadsProvider
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        } else if (isMediaDocument(uri)) {
            // MediaProvider
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // MediaStore (and general)

        String res = getDataColumn(context, uri, null, null);
        // Return the remote address
        if (res == null && isGooglePhotosUri(uri))
            return uri.getLastPathSegment();
        else
            return res;

    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        // File
        return uri.getPath();
    }

    return null;
}

    /**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 * @author paulburke
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 * @author paulburke
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 * @author paulburke
 */
public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority()) ||
            "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
}

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @param selection (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 * @author paulburke
 */
public static String getDataColumn(Context context, Uri uri, String selection,
                                   String[] selectionArgs) {

    final String column = "_data";
    final String[] projection = { column };

    try (Cursor cursor =
          context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {
        if (cursor != null && cursor.moveToFirst()) {
            if (BuildConfig.DEBUG)
                DatabaseUtils.dumpCursor(cursor);

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    }
    return null;
}

As far as I remember the main thing was to figure out the fix to his isGooglePhotosUri() method

Community
  • 1
  • 1
Kirill K
  • 771
  • 6
  • 17