2012-07-06 51 views
1

我試圖執行以下任務:空指針異常而解碼位圖圖像

  1. 拍攝圖像
  2. 儲存於SD卡,並保存在一個字符串的路徑。

代碼:

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"); 
          } 

     } 

根據該法,我以一個圖像,然後又做了thumbimage顯示它的形象圖也存儲這個thumbImage。但是發生錯誤的地方是

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

它返回null。任何人都可以解釋發生了什麼//?

回答

4

您只能使用InputStream一次!

當你調用

BitmapFactory.decodeStream(is2 ,null, options); 

將 「消費」 is2。爲了得到

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

工作,你必須爲它創建一個新的InputStream,並用它明確地存在。

0

試試這個。

ContentResolver cr = mContext.getContentResolver();

用cr.openInputStream(uri)替換is2;

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

它對我來說工作正常。

-1

NullPointerException當程序中使用的變量中沒有數據/值時,會發生這種情況。因此,請查看您的圖片網址或位置是否正確,因爲有時JRE無法找到圖片或使用圖片,並提供NullPointerException。它總是發生在我身上。