can i get image file width and height from uri in android?

前端 未结 10 2056
深忆病人
深忆病人 2020-12-08 01:58

i can getting the image width through MediaStore.Images.Media normally

but i need to getting the image width and height from image which selected from d

相关标签:
10条回答
  • 2020-12-08 02:43

    Blackbelt's answer will work most of the time using Options, but I would like to propose another solution or fallback solution by using the ExifInterface. If you have the image URI, you can create the ExifInterface using the full path, no need for Bitmap object nor BitmapFactory.Options.

    ex.

    int width = exif.getAttributeInt( ExifInterface.TAG_IMAGE_WIDTH, defaultValue );
    int height = exif.getAttributeInt( ExifInterface.TAG_IMAGE_LENGTH, defaultValue ); 
    
    0 讨论(0)
  • 2020-12-08 02:44

    If you guys keep getting (0,0) in the width and height, you probably want to decode a stream, and not a file.

    It probably because you're trying to read an image from the assets. Try this:

    val b = BitmapFactory.decodeStream(context.assets.open("path/in/assets/img.png"))
    val width = b.width
    val height = b.height
    
    0 讨论(0)
  • 2020-12-08 02:48

    This set of utilities to work with the Size abstraction in Android.

    It contains an class SizeFromImage.java You can use it like this:

    ISize size = new SizeFromImage(imgFilePath);
    size.width();
    size.hight();
    
    0 讨论(0)
  • 2020-12-08 02:52
    private void getDropboxIMGSize(Uri uri){
       BitmapFactory.Options options = new BitmapFactory.Options();
       options.inJustDecodeBounds = true;
       BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);
       int imageHeight = options.outHeight;
       int imageWidth = options.outWidth;
     
    }
    

    no there is no way. You have to create a Bitmap object. if you use the inJustDecodeBounds flag the bitmap would not be loaded in memory. In fact BitmapFactory.decodeFile will return null. In my example uri is the phisical path to the image

    0 讨论(0)
  • 2020-12-08 02:54

    For folks having content uri (starting with content://), open an InputStream & decode that stream to avoid getting 0 width and height. I'll use Kotlin

    val uri: Uri = ....
    
    val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
    val inputStream = contentResolver.openInputStream(uri)
    BitmapFactory.decodeStream(inputStream, null, options)
    
    val width = options.outWidth
    val height = options.outHeight
    
    0 讨论(0)
  • 2020-12-08 02:55

    In addition to @Blackbelt answer you should use the following code to retrieve file path from Uri:

    public static String getPathFromURI(Context context, Uri contentUri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
                DocumentsContract.isDocumentUri(context, contentUri)) {
            return getPathForV19AndUp(context, contentUri);
        } else {
            return getPathForPreV19(context, contentUri);
        }
    }
    
    private static String getPathForPreV19(Context context, Uri contentUri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            try {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                return cursor.getString(columnIndex);
            } finally {
                cursor.close();
            }
        }
    
        return null;
    }
    
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static String getPathForV19AndUp(Context context, Uri contentUri) {
        String documentId = DocumentsContract.getDocumentId(contentUri);
        String id = documentId.split(":")[1];
    
        String[] column = { MediaStore.Images.Media.DATA };
        String sel = MediaStore.Images.Media._ID + "=?";
        Cursor cursor = context.getContentResolver().
                query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        column, sel, new String[]{ id }, null);
    
        if (cursor != null) {
            try {
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    return cursor.getString(columnIndex);
                }
            } finally {
                cursor.close();
            }
        }
    
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题