2015-12-17 44 views
0

我正在从“图库”或“拍照”中选择图像。我的问题是,当使用相机拍照时重新按下。我收到错误。尝试在空对象引用上调用虚拟方法'int android.graphics.Bitmap.getHeight()'

我的代码是

if (items[position].equals("Take photo")) { 
         Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         File file= getOutputMediaFile(); 
         Uri picUri = Uri.fromFile(file); 
         filepath = picUri.getPath(); 
         i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); 
         _a.startActivityForResult(i, 1); 
        } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    Log.e("Action", action); 
    if (action.equals("add_car")) { 
     if(_addLayout.filepath != null){ 
      filePath = _addLayout.filepath; 
      _addLayout.filepath = null; 
     } 
     if(data != null) 
     filePath = CommonUtilities.getPath(data.getData(), "Image"); 
     _addLayout.filepath = filePath; 
     setImage(UI_AddCar.ivTakenPicture); 
    } 
    } 

void setImage(ImageView im){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(filePath); 

    if(filePath != null){ 
     int height = bitmap.getHeight(), width = bitmap.getWidth(); 
      if (height > 1280 && width > 960){ 
       Bitmap imgbitmap = BitmapFactory.decodeFile(filePath, options); 
       im.setImageBitmap(imgbitmap); 
       im.setVisibility(View.VISIBLE); 
     } else { 
       im.setImageBitmap(bitmap); 
       im.setVisibility(View.VISIBLE); 
      } 
    } 

在这种UI_AddCar.ivTakenPicture是ImageView的

+1

您的位图为空。在下面的日志中打印位图的高度'int height = bitmap.getHeight()' – Piyush

+0

谢谢你的解决方案我通过改变if条件来实现这个if(filePath!= null && bitmap!= null){ \t \t \t int height = bitmap.getHeight(),width = bitmap.getWidth(); \t \t if(height> 1280 && width> 960){ – Bahu

回答

2

由于皮什·古普塔。我更新如果条件像

if(filePath != null && bitmap != null){ 
     int height = bitmap.getHeight(), width = bitmap.getWidth(); 

      if (height > 1280 && width > 960){ 

        Bitmap imgbitmap = BitmapFactory.decodeFile(filePath, options); 
        im.setImageBitmap(imgbitmap); 
        im.setVisibility(View.VISIBLE); 

      }else { 

        im.setImageBitmap(bitmap); 
        im.setVisibility(View.VISIBLE); 
      } 
    } 
相关问题