【问题标题】:Set image taken from camera into an ImageView将从相机拍摄的图像设置为 ImageView
【发布时间】:2014-01-20 16:56:51
【问题描述】:

我正在尝试将从相机拍摄的图像设置为 ImageView。我启动相机意图,然后在 OnActivityResult 中收到 NullPointerException,我不明白该错误。

在这里,我启动相机意图并将图像存储在手机的图库中:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST);

现在,相机的图像令牌保存在手机的图库中(高质量)。 在 onActivityResult 中,我想将图像放入 ImageView。这是我的代码:

else if (requestCode == CAMERA_REQUEST)
{
  if (resultCode == RESULT_OK)
   {
     imageUri = data.getData();
             try 
             {
               Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); //NullPointerException
               myImageView.setImageBitmap(bitmap);
             } 
          catch (IOException e) 
             {
              e.printStackTrace();
             }

我有一个 NullPointerException,为什么?请问怎么解决?

【问题讨论】:

    标签: android android-intent bitmap camera imageview


    【解决方案1】:

    这会将您的结果保存在 mediaStore 中,因此 data.getData(); 将返回 NULL

    pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    删除它,您将获得在 ImageView 上显示的数据。但是,如果您直接从data 获取图像,则图像质量很差

    【讨论】:

    • 没错,我想通过邮件高质量发送这张图片。所以我需要ImageUri。请问我可以这样做吗?
    • 直接使用 imageUri 而不是使用data.getData();。您可以从 imageUri 获取路径并使用BitmapFactory.decodeFile(path) 创建位图
    • 将 parentActionIntent 更改为意图。并将第二部分更改为:if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()); myImageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } 我不完全了解您的代码,所以我不确定它是否有效。如果需要,我可以为此目的发布示例代码
    • 感谢您的回复。但这对我不起作用
    【解决方案2】:

    您的代码不完整,您还没有添加 logcat 错误! 所以在这种情况下很难指导你!但是你想要达到的并不是那么难。试试这个:

    public class LaunchCamera extends Activity {
     ImageView imVCature_pic;
     Button btnCapture;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_launch_camera);
      initializeControls();
     }
    
     private void initializeControls() {
      imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
      btnCapture=(Button)findViewById(R.id.btnCapture);
      btnCapture.setOnClickListener(new OnClickListener() {
    
       @Override
       public void onClick(View v) {
        /* create an instance of intent
         * pass action android.media.action.IMAGE_CAPTURE 
         * as argument to launch camera
         */
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        /*create instance of File with name img.jpg*/
        File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
        /*put uri as extra in intent object*/
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        /*start activity for result pass intent as argument and request code */
        startActivityForResult(intent, 1);
       }
      });
    
     }
    
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      //if request code is same we pass as argument in startActivityForResult
      if(requestCode==1){
       //create instance of File with same name we created before to get image from storage
       File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
       //get bitmap from path with size of
       imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
       }
     }
     public static Bitmap decodeSampledBitmapFromFile(String path,
       int reqWidth, int reqHeight) { 
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      //Query bitmap without allocating memory
      options.inJustDecodeBounds = true;
      //decode file from path
      BitmapFactory.decodeFile(path, options);
      // Calculate inSampleSize
      // Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      //decode according to configuration or according best match
      options.inPreferredConfig = Bitmap.Config.RGB_565;
      int inSampleSize = 1;
      if (height > reqHeight) {
       inSampleSize = Math.round((float)height / (float)reqHeight);
      }
      int expectedWidth = width / inSampleSize;
      if (expectedWidth > reqWidth) {
       //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
       inSampleSize = Math.round((float)width / (float)reqWidth);
      }
      //if value is greater than 1,sub sample the original image
      options.inSampleSize = inSampleSize;
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeFile(path, options);
     }
    }
    

    完整的代码sn-p描述得很好here

    【讨论】:

    • 非常感谢您提供的链接。但是,我需要 imageUri 通过电子邮件从相机发送图像令牌。请问我该怎么做?
    • 请为答案投票!为他人提供帮助!好的,这个链接会帮助你!在电子邮件发送中。 stackoverflow.com/questions/1247983/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2015-03-07
    相关资源
    最近更新 更多