3

我正在尝试保存从相机拍摄的图像,然后将其连同其拇指一起存储在 sdCard 上,该拇指也在 imageView 上显示。但是它在 Null 指针处给出错误

 imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false);

怎么了?

    {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            try 
            {
              if (title.getText().toString().equals(""))
              {
                displayAlert("Please Input Title First","Error!");
              }
              else
              {

                Integer val = myMisc.miscId ;
                String fileName = "image" + "_" + title.getText().toString()+"_" + val.toString();
                photo = this.createFile(fileName, ".jpg");
                myMisc.filename = photo.getAbsolutePath();
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                startActivityForResult(intent, RESULT_CAMERA_SELECT);
              }
            }
            catch(Exception e)
            {
                Log.v("Error", "Can't create file to take picture!");
                displayAlert("Can't create file to take picture!","SDCard Error!");
            }
        }




public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data) 
    {
       if (resultCode == Activity.RESULT_OK) 
       {
           if (requestCode == RESULT_CAMERA_SELECT)
           {
               try 
               {
                   saveImage();
               }
               catch (IOException e) 
               {
                   e.printStackTrace();
               }
           }



public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

    public void saveImage() throws IOException
    {
      try 
        {       
           FileInputStream is2 = new FileInputStream(photo);
           final BitmapFactory.Options options = new BitmapFactory.Options();
           options.inJustDecodeBounds = true;
           Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options);
           options.inSampleSize = calculateInSampleSize(options, 40, 40);
           options.inJustDecodeBounds = false;
           imageBitmap = BitmapFactory.decodeStream(is2 ,null, options);
           imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false);
           Integer val = myMisc.miscId;
           String fileName = ".thumbImage" + "_" + title.getText().toString()+ "_" + val.toString();  
           photo = this.createFile(fileName, ".jpg");
           myMisc.thumbFileName = photo.getAbsolutePath();
        try {
           FileOutputStream out = new FileOutputStream(photo);
           imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            } catch (Exception e) {
           e.printStackTrace();
            }
            is2.close();
            Uri uri = Uri.fromFile(photo);
            photo = null;
            imageBitmap = null;
            imageView.setImageURI(uri);
        }catch(Exception e)
        {
            displayAlert("Can't create file to take picture!","SD Card Error");
        }
    }
4

2 回答 2

0

我发现我的错误Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options);解码两次

于 2013-02-09T03:57:32.730 回答
0

这可能是因为 decodeStream 方法。

Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options);

您在请求 Rectangle 时解析了 null 并且当您尝试创建缩放位图时它无效。

于 2013-02-01T22:11:29.580 回答