简体   繁体   English

解码位图图像时出现空指针异常

[英]Null pointer exception while decoding bitmap image

I am trying to implement the following task: 我正在尝试执行以下任务:

  1. Take an Image 拍一张照片
  2. Store it in sd card and save the path in a String. 将其存储在SD卡中并将路径保存在String中。

code: 码:

public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data) 
    {
       if (resultCode == Activity.RESULT_OK) 
       {
           if (requestCode == RESULT_CAMERA_SELECT)
           {
               try 
               {
                   photo = null;
                   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
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            FileInputStream is2 = new FileInputStream(new File(myReceipt.filename));
            BitmapFactory.decodeStream(is2 ,null, options);
            // Here is2 get file with width of pic as 2000*1500 etc

            options.inSampleSize = calculateInSampleSize(options, 100, 100);

            options.inJustDecodeBounds = false;

// PROBLEM EXISTS AT THIS POINT. imageBitmap is returned as null.....


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

            try{

                        photo = this.createFile(fileName, ".jpg");
                        thumbFileName = photo.getAbsolutePath();
                        Uri uri = Uri.fromFile(photo);

                        try {
                               FileOutputStream out = new FileOutputStream(photo);
                               imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                                } catch (Exception e) {
                               e.printStackTrace();
                                }
                                is2.close();
                                photo = null;
                                imageBitmap = null;
                                imageView.setImageURI(uri);
                            }catch(Exception e)
                            {
                                displayAlert("Can't create file to take picture!","SD Card Error");
                            }

        }

According to the code, I am taking an image, then making a thumbimage to show it in an image view and also storing this thumbImage. 根据代码,我正在拍摄一张图片,然后制作一个拇指图像以在图像视图中显示它并存储此thumbImage。 But error occurs at 但错误发生在

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

it returns null. 它返回null。 Can anyone explain what is going on//? 任何人都可以解释发生了什么//?

You can use an InputStream only once! 您只能使用一次InputStream

When you call 你打电话时

BitmapFactory.decodeStream(is2 ,null, options);

it will "consume" is2 . 它将“消耗” is2 To get 要得到

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

working, you have to create a new InputStream for it, and use it explicitly there. 在工作中,你必须为它创建一个新的InputStream ,并在那里显式使用它。

Try this. 尝试这个。

ContentResolver cr = mContext.getContentResolver(); ContentResolver cr = mContext.getContentResolver();

Replace is2 with cr.openInputStream(uri); 用cr.openInputStream(uri)替换is2;

Bitmap imageBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri) ,null, options); Bitmap imageBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),null,options);

It is working fine for me. 它对我来说很好。

NullPointerException s occur when the variable used in the program has no data/value in it. 当程序中使用的变量中没有数据/值时,会发生NullPointerException So, see if your image URL or location is correct, because sometimes the JRE cannot find the image or use the image and gives a NullPointerException . 因此,请查看您的图像URL或位置是否正确,因为有时JRE无法找到图像或使用图像并给出NullPointerException It happens to me all the time. 它总是发生在我身上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM