【问题标题】:Get width and Height from URI of picture从图片的URI获取宽度和高度
【发布时间】:2017-03-19 07:56:28
【问题描述】:

我正在尝试获取图片的宽度和高度,以便能够将其加载到我的活动的画布中。当我尝试打印此高度和宽度时,我总是得到 0。我不明白为什么?

这是我的代码:

    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);
    }

【问题讨论】:

    标签: android image width uri


    【解决方案1】:
    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多