2012-06-21 69 views
5

我的第一個問題在這裏!我遇到了一段代碼,該代碼用額外的輸出選項啓動攝像機意圖,然後在活動結果嘗試調整該圖像的大小。發生的事情是在回調中的resize函數中拋出一個空指針異常。Android圖像調整後的照相機意圖回調

原始大圖仍然保存在文件系統上,因爲我可以從文件系統訪問它。

原始相機jpeg的尺寸是2560x1920,使用的手機是谷歌Nexus One。

我沒有清楚的想法,爲什麼調整大小不工作,任何人有任何見解?

下面是一些代碼:

的takePicture功能還創建虛擬文件:

 public boolean takePicture() { 

      Log.e(TAG, "takePicture interface function"); 
      String FileUri = Environment.getExternalStorageDirectory() + "/samples/"; 
      File file = new File(FileUri,"picture"+ pictureNumber +".jpg"); 
      try { 
       file.createNewFile(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } 
       Uri outputFileUri = Uri.fromFile(file); 

      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      Log.e(TAG, "intent started"); 
      return true; 
     } 

的對活動結果的回調函數:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     // handle the camera request returns and handle back button in camera. 

     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_CANCELED) { 
      Toast toast = Toast.makeText(this,"Canceled, no picture taken.", 1000); 
      toast.show(); 
      return; 
      } 

     else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

      Log.e(TAG, "Camera intent return"); 

      Bitmap scaledphoto = null; 
      int height = 300; 
      int width = 300;  

      Bitmap photo = BitmapFactory.decodeFile(APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg"); 
      Log.e(TAG, "Picture fetched"); 

      scaledphoto = Bitmap.createScaledBitmap(photo, height, width, true); 


      Log.e(TAG, "Picture scaled"); 

      saveImageToFile(scaledphoto, "picture" + pictureNumber + ".jpg"); 


      Log.e(TAG, "Scaled picture saved"); 

      myWebView.loadUrl("javascript:pictureTaken(\""+ pictureLoc + "\")"); 

      pictureNumber++; 

而這裏的logcat的:

06-21 14:59:13.496: E/AndroidRuntime(6130): FATAL EXCEPTION: main 
06-21 14:59:13.496: E/AndroidRuntime(6130): java.lang.RuntimeException: Failure  delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {test.test/test.test.CameraIntentTestActivity}: java.lang.NullPointerException 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.app.ActivityThread.access$1100(ActivityThread.java:123) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.os.Looper.loop(Looper.java:137) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at dalvik.system.NativeStart.main(Native Method) 
06-21 14:59:13.496: E/AndroidRuntime(6130): Caused by: java.lang.NullPointerException 
06-21 14:59:13.496: E/AndroidRuntime(6130):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432) 

回答

2

變化

Bitmap photo = BitmapFactory.decodeFile(APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg"); 

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
+0

我用這種方法得到的所有圖片似乎是大型模糊彷彿從一個較小的位圖放大的,而被直接保存到文件夾額外的圖片輸出似乎是全分辨率的JPEG。它的工作原理,但似乎並不是獲得中等分辨率JPEG的最佳方式,即400x400。 – TeraTon

+0

恐怕由於imageSize而無法解碼。 –

+0

是的,它似乎像圖像大小隻是其失敗的原因,因爲通過stackoverflow搜索,我發現了幾個同樣的問題的傢伙。謝謝你的提示! – TeraTon