2016-01-14 224 views
2

我正在创建运行时图像视图,并根据图像设置宽度和高度,其工作完美低于android棉花糖,但它不工作在棉花糖。bitmap.createscaledbitmap不能在棉花糖

我检查我的图像不为空,位图也不为空。相同的代码工作正常,并根据我显示图像..但是,当此代码运行棉花糖然后给出错误。我也给了所有的同情。 erroe此行image.getMeasuredWidth()此行给出了错误

这是我的代码 -

image.setImageBitmap(Bitmap.createScaledBitmap(bitmap, image.getMeasuredWidth(), image.getMeasuredWidth(), false)); 

这段代码给了错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
      at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:610) 
+0

[从sdcard读取图像文件到位图,为什么会出现NullPointerException?](http://stackoverflow.com/questions/8710515/reading-an-image-file-into-bitmap-from -sdcard-why-am -i-getting-a-nullpointerexc) – OBX

+0

相同的代码工作正常在下面的Android 6 –

+0

我检查图像不为空 –

回答

1

检查这个答案。

单击相机按钮时。

public void getPhotoFromCamera() { 

    if (!marshMallowPermission.checkPermissionForCamera()) { 
     marshMallowPermission.requestPermissionForCamera(); 
     marshMallowPermission.requestPermissionForExternalStorage(); 
     Toast.makeText(this,"camera permition",Toast.LENGTH_SHORT).show(); 
    } else { 
     { 
      Toast.makeText(this,"1111111111",Toast.LENGTH_SHORT).show(); 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      File mediaStorageDir = new File(
        Environment.getExternalStorageDirectory() 
          + File.separator 
          + "IMG_" 
          + File.separator 
          + ".jpg" 
      ); 

      if (!mediaStorageDir.exists()) { 
       mediaStorageDir.mkdirs(); 
      } 

      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
        Locale.getDefault()).format(new Date()); 
      try { 
       File mediaFile = File.createTempFile(
         "IMG_" + timeStamp, /* prefix */ 
         ".jpg",   /* suffix */ 
         mediaStorageDir  /* directory */ 
       ); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile)); 
       startActivityForResult(takePictureIntent, 100); 

       Log.e("STRING",mediaFile.getPath()+" String "+mediaFile); 

       fileUri=Uri.fromFile(mediaFile); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

,并添加这也

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Log.e("String", resultCode + " " + requestCode); 


     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       previewCapturedImage(); 
      } else if (resultCode == RESULT_CANCELED) { 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 

      Log.e("Camera111", "2222222222"); 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      camera = BitmapFactory.decodeFile(picturePath); 
      Log.e("Camera111", "" + camera); 
      setimage(camera); 
     } 
    } 

    private void setimage(Bitmap bitmap) { 

     if (text == null) { 
      ((ImageView) findViewById(R.id.restro_image)).setImageBitmap(bitmap); 
     } else { 
      text.setMaxHeight(text.getMeasuredWidth()); 
      if (picturePath != null) { 

       text.setImageBitmap(Bitmap.createScaledBitmap(bitmap, text.getMeasuredWidth(), text.getMeasuredWidth(), false)); 
      } else { 
       text.setImageBitmap(Bitmap.createScaledBitmap(bitmap, text.getMeasuredWidth(), text.getMeasuredWidth(), false)); 
//    text.setImageBitmap(bitmap); 
      } 
     } 
    } 

我希望这是你的工作。

+0

谢谢。我有些错误,但这是解决.. –