2012-05-30 82 views
2

我正在整合facebook與android,我想在拍攝照片時將其保存到SD卡,然後將其上傳到Facebook。來自android相機意圖問題的圖像

這裏是我的代碼:

photo_up=(Button)findViewById(R.id.camera_foto_button); 
      photo_up.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(PlaceScreen.this))); 
         startActivityForResult(intent,CAMERA_REQUEST); 
        } 
       }); 

private File getTempFile(Context context){ 
      //it will return /sdcard/image.tmp 
      final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName()); 
      if(!path.exists()){ 
      path.mkdir(); 
      } 
      return new File(path, "image.png"); 
     } 

和OnActivity結果

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode){ 
       case CAMERA_REQUEST:{ 
        final File file = getTempFile(this); 
        try { 
         bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file)); 
         // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc) 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 


        ByteArrayOutputStream stream = new ByteArrayOutputStream();   
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);   
        byteArray = stream.toByteArray(); // convert camera photo to byte array 
        Bundle params = new Bundle();  
        params.putByteArray("picture", byteArray);  
        params.putString("message", "Have fun");  
        Utility.mAsyncRunner.request("me/photos", params, 
          "POST", new PhotoUploadListener(), null); 
        break; 
       } 

那麼什麼情況是這樣的:攝像頭打開,imagae捕獲並保存,但我親近力,這是不上傳到fb。

我在手機上也檢查過它。 logcat的是在這裏:

05-31 02:50:19.437: E/AndroidRuntime(2470): FATAL EXCEPTION: main 
05-31 02:50:19.437: E/AndroidRuntime(2470): java.lang.RuntimeException: Unable to resume activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.NullPointerException 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.access$1600(ActivityThread.java:117) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.os.Looper.loop(Looper.java:130) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at dalvik.system.NativeStart.main(Native Method) 
05-31 02:50:19.437: E/AndroidRuntime(2470): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.NullPointerException 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2536) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  ... 13 more 
05-31 02:50:19.437: E/AndroidRuntime(2470): Caused by: java.lang.NullPointerException 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at com.myname.package.PlaceScreen.onActivityResult(PlaceScreen.java:325) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 
05-31 02:50:19.437: E/AndroidRuntime(2470):  ... 14 more 

編輯:

好吧我BMP哪裏是空。這是爲什麼?什麼是錯誤的?

bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file)); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

任何人都可以找出爲什麼我的bmp爲空,而我可以在畫廊中看到圖像。這意味着圖像是正常拍攝的,但沒有任何東西會因爲bmp = MediaStore.Images.Media

+0

異常提供了一個非常明確的提示:PlaceScreen.java的第325行的內容爲空。 – yorkw

+0

好的我的bmp在null中。這是爲什麼?什麼是錯誤的? bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.fromFile(file)); \t \t \t \t bmp.compress(Bitmap.CompressFormat.PNG,100,stream); – ghostrider

+0

圍繞'getBitmap()'方法的try/catch塊是否觸發'FileNotFoundException'或'IOException'?你能否使用DDMS驗證該文件是否在SD卡上的請求位置(我知道你可以在庫中看到它,但這並不意味着它被保存在你要求保存的地方)。 – Devunwired

回答

4

雖然使用網絡上可用的通用代碼調用相機並拍照並回收它,但我在SAMSUNG設備中遇到了一些問題那時同樣的錯誤發生在Failure delivering result ResultInfo,並且找不到任何解決方案,可能是因爲任何API級別或設備依賴。

但要克服它,我寫我的代碼另一種方式做任務,該代碼是在這裏:

1)通話攝像頭

 String _path = Environment.getExternalStorageDirectory() 
     + File.separator + "TakenFromCamera.png"; 
     File file = new File(_path); 
     Uri outputFileUri = Uri.fromFile(file); 
     Intent intent = new Intent(
     android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, 1212); 

2)在ActivityResult得到位圖

  if (requestCode == 1212) { 
       String _path = Environment.getExternalStorageDirectory() 
       + File.separator + "TakenFromCamera.png"; 
       mBitmap = BitmapFactory.decodeFile(_path); 
       if (mBitmap == null) { 
        // bitmap still null 
       } else { 
        // set bitmap in imageview 
       } 
      } 
+0

它不工作。位圖始終爲空,沒有任何原因。權限 - 好的 – Peter