【问题标题】:Issue in Decoding a Bitmap解码位图的问题
【发布时间】:2013-06-02 15:51:45
【问题描述】:

我正在解码位图并在画布背景中使用图像。当我使用解码的图像绘制背景时,我得到了空指针异常。请指出我的代码需要更正的地方。

以下是我用于解码的代码

    public  Bitmap getAssetImage(Context context, String filename) throws IOException {
        AssetManager assets = getApplicationContext().getResources().getAssets();
        InputStream buffer = null;
        try {
            buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
         //Decode image size
         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream(buffer,null,o);

         //The new size we want to scale to
         final int REQUIRED_WIDTH= (int) dWidth;
         final int REQUIRED_HIGHT= (int) dHeight;
         //Find the correct scale value. It should be the power of 2.
         int scale=1;
         while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
             scale*=2; 
         //Decode with inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize=scale;
         return BitmapFactory.decodeStream(buffer, null, o2);   
    } 


    public void run() {
        // TODO Auto-generated method stub
//      Log.i("Notice", "In run of mybringback"); 
        if(backgoundImage == null){ 
            try {Log.i("MyBringBack", "In run of mybringback.. getting the image of background"); 
                backgoundImage = getAssetImage(getApplicationContext(),"backgroundhomepage");  
                System.gc();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ourHolder = getHolder();
        while (isRunning) {
//           
            if (!ourHolder.getSurface().isValid()){
                continue;
            } 
            canvas = ourHolder.lockCanvas();    
            screenCenterX = dWidth / 2;
            screenCenterY = dHeight / 2; 
            canvas.drawBitmap(backgoundImage, 0, 0, null);   
            if (imagePublishDone) {
                if(!welcomeDone){ 
                    message = "Drop your wish to panda";
                    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
                    welcomeDone=true;
                }
                moveImageInEllipticalPath();
            } else {
                initialImagePublish();
            }

            centreReached = false;
            ourHolder.unlockCanvasAndPost(canvas);
        }
    } 

LogCat:

06-02 11:06:01.469: E/AndroidRuntime(7787): FATAL EXCEPTION: Thread-565
06-02 11:06:01.469: E/AndroidRuntime(7787): java.lang.NullPointerException
06-02 11:06:01.469: E/AndroidRuntime(7787):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:1037)
06-02 11:06:01.469: E/AndroidRuntime(7787):     at android.graphics.Canvas.drawBitmap(Canvas.java:1078)
06-02 11:06:01.469: E/AndroidRuntime(7787):     at com.example.funandlearn.DragDrop$MyBringBackSurface.run(DragDrop.java:638)
06-02 11:06:01.469: E/AndroidRuntime(7787):     at java.lang.Thread.run(Thread.java:856)

【问题讨论】:

  • 检查位图背景图像是否为空。
  • 你能显示 decodeStream() 代码吗
  • 我没有覆盖它。这是基础版
  • 检查你的缓冲区是否为空

标签: android android-imageview android-image


【解决方案1】:

你需要关闭你在第一次解码操作中打开并使用的缓冲区(缓冲区不能被读取两次) 然后再次重新打开它以进行第二次操作,然后它应该可以工作了

 public  Bitmap getAssetImage(Context context, String filename) throws IOException {
    AssetManager assets = getApplicationContext().getResources().getAssets();
    InputStream buffer = null;
    InputStream buffer1 = null; //<----added this
    try {
        buffer = new BufferedInputStream((assets.open("drawable/"+filename +".png")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
     //Decode image size
     BitmapFactory.Options o = new BitmapFactory.Options();
     o.inJustDecodeBounds = true;
     BitmapFactory.decodeStream(buffer,null,o);
     try{                      //<----added this
         buffer.close();
      } catch(IOException e){
             }
     //The new size we want to scale to
     final int REQUIRED_WIDTH= (int) dWidth;
     final int REQUIRED_HIGHT= (int) dHeight;
     //Find the correct scale value. It should be the power of 2.
     int scale=1;
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
         scale*=2; 
     //Decode with inSampleSize
            try { //<----added this
      buffer1 = new BufferedInputStream((assets.open("drawable/" + filename +".png")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     BitmapFactory.Options o2 = new BitmapFactory.Options();
     o2.inSampleSize=scale;
     Bitmap bitamp = BitmapFactory.decodeStream(buffer1, null, o2); //<----added this
      try{ //<----added this
           buffer1.close();
     } catch(IOException e){}
     return bitmap; //<----added this  
} 

【讨论】:

  • 屏幕占满了图像的 80%。剩余 % 的图像在屏幕之外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 2020-09-10
相关资源
最近更新 更多