简体   繁体   English

从图片的URI获取宽度和高度

[英]Get width and Height from URI of picture

I am trying to get the widht and the Height of a picture to then be able to load it into a canvas on my activity. 我试图获取图片的宽度和高度,然后将其加载到我的活动中的画布中。 When I try to print this Height and Width I always get 0. I do not understand why ? 当我尝试打印此高度和宽度时,我总是得到0。我不明白为什么?

Here is my code: 这是我的代码:

    private void openPicture() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            InputStream stream = context.getContentResolver().openInputStream(uri);
            BitmapFactory.decodeStream(stream);
            int width = options.outWidth;
            int height = options.outHeight;
            Log.d("DEBUG", ""+ width);
            loadPicture(width, height);
        } catch(IOException e) {
            Log.e("FingerPainterView", e.toString());
        }
    }

    private void loadPicture(int w, int h) {

        try {
            // attempt to load the uri provided, scale to fit our canvas
            InputStream stream = context.getContentResolver().openInputStream(uri);
            Bitmap bm = BitmapFactory.decodeStream(stream);
            bitmap  = Bitmap.createScaledBitmap(bm, Math.max(w, h), Math.max(w, h), false);
            stream.close();
            bm.recycle();
        } catch(IOException e) {
            Log.e("FingerPainterView", e.toString());
        }
        canvas = new Canvas(bitmap);
    }
private void getImageWidthAndHeight(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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM