user2423476
user2423476

Reputation: 2275

Get Image Width and Height from URI

Is it possible to get the width and height of a image file from its URI. I was trying to use this code but theirs a error: has a syntax error after the getAbsolutePath()

Syntax error on token ")", invalid ArgumentList

private void getDropboxIMGSize(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
}

Upvotes: 4

Views: 9401

Answers (3)

hasanghaforian
hasanghaforian

Reputation: 14042

I had the same problem and I found it's solution at this answer.You have to use

BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);

in your code instead of this:

BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);

Upvotes: 1

ataulm
ataulm

Reputation: 15334

If you extract your arguments into local variables, you'll be less likely to miss a parenthesis/include an extra one, and it'll be easier to read your code.

Before:

private void getDropboxIMGSize(Uri uri){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
}

After:

private void getDropboxIMGSize(Uri uri){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    String path = uri.getPath().getAbsolutePath();
    BitmapFactory.decodeFile(path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
}

Note, you've assigned options.outHeight and options.outWidth to local variables and then the method ends; you're not doing anything with these values.

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Do this way

public void getDropboxIMGSize(Uri uri) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(getAbsolutePath(uri), o);
        int imageHeight = o.outHeight;
        int imageWidth = o.outWidth;
    }



public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

Upvotes: 1

Related Questions