2016-11-05 12 views
0

画像の高さと高さを取得しようとしていますが、これをキャンバスにアクティビティで読み込むことができます。この高さと幅を印刷しようとすると、私はいつも0を得ます。私はなぜそれを理解できませんか?ここで画像のURIから幅と高さを取得する

は私のコードです:

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

答えて

0
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; 

} 
関連する問題