2013-03-06 182 views
-2

如何將照相機拍攝的照片通過我的應用程序保存到設備的圖庫中?Android應用程序在圖庫中保存和存儲圖像?

問題是,應用程序目前在拍攝照片後崩潰,並且只能保存指定目錄中的單個圖像。 我是否認爲應用程序崩潰是因爲它不知道在保存文件後該做什麼?我該如何解決這個問題,return語句之後的任何內容都是「無法訪問的代碼」。

代碼:

public class Activity_Camera extends Activity implements View.OnClickListener { 

     public static final int cameraData = 1; 

     ImageButton ib; 
     ImageView iv; 
     Intent i; 
     Bitmap bmp; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_camera); 
       initialise(); 
     } 

     private void initialise() { 
       iv = (ImageView) findViewById(R.id.ivPicReturn); 
       ib = (ImageButton) findViewById(R.id.ibTakePic); 

       ib.setOnClickListener(this); 
     } 

     public void onClick(View v) { 
       switch (v.getId()) { 
       case R.id.ibTakePic: 
         i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         // 
         i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());    
         // 
         startActivityForResult(i, cameraData); 
         break; 
       } 
     } 
     // 
     private Uri getOutputMediaFileUri() { 
      File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/TradeIt"); 
      imagesFolder.mkdirs(); 
      File image = new File(imagesFolder, "image.jpg"); 
      Uri uriSavedImage = Uri.fromFile(image); 
      return uriSavedImage; 


     } 
     // 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 
       if (resultCode == RESULT_OK) { 
         Bundle extras = data.getExtras(); 
         bmp = (Bitmap) extras.get("data"); 
         iv.setImageBitmap(bmp); 


       } 

     } 

} 

logcat的

W/dalvikvm(919): threadid=1: thread exiting with uncaught exception (group=0x41d7d930) 
03-07 00:00:24.704: E/AndroidRuntime(919): FATAL EXCEPTION: main 
03-07 00:00:24.704: E/AndroidRuntime(919): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.hyperlocal.tradeit/com.hyperlocal.tradeit.Activity_Camera}: java.lang.NullPointerException 
03-07 00:00:24.704: E/AndroidRuntime(919): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319) 
+0

發佈logcat將有助於 – DigCamara 2013-03-06 23:57:41

+0

@DigCamara 03-07 00:00:24.694:W/dalvikvm(919):threadid = 1 :線程退出未捕獲的異常(組= 0x41d7d930) 03-07 00:00:24.704:E/AndroidRuntime(919):致命例外:main 03-07 00:00:24.704:E/AndroidRuntime(919) :java.lang.RuntimeException:將結果resultInfo {who = null,request = 1,result = -1,data = null}傳遞給activity {com.hyperlocal.tradeit/com.hyperlocal.tradeit.Activity_Camera}:失敗.NullPointerException 03-07 00:00:24.704:E/AndroidRuntime(919):\t at android.app.Act ivityThread.deliverResults(ActivityThread.java:3319) – Rav 2013-03-07 00:04:45

回答

1

,你必須增加一個變量並保存,並把它添加到您的文件名時,你得到URI,使更多的圖片,併爲圖片在您的畫廊顯示這樣做:

sendBroadcast(new Intent(
       Intent.ACTION_MEDIA_MOUNTED, 
       Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

如果你不sentBroadcast這樣AFTE r你拍照的畫廊將不會更新,直到下次打開電話

相關問題