2013-04-05 49 views
3

對於大學我必須開發一個應用程序在Android與臉部檢測。 爲此,我必須將各種照片保存在我的畫廊中。 問題是保存照片後,圖庫不會更新。 更確切地說,如果我刪除要保存圖像的目錄,我打開應用程序並拍攝照片,然後進入圖庫,「更新」後我可以看到照片。 但是一旦我擁有目錄,如果我拍攝一張新照片,這並不會覆蓋舊照片。如何在照片後更新Android圖庫?

網上我找到了這一點:

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

...但它不工作!

這是我的代碼:

. 
    . 
    . 
     ImageButton buttonPicture = (ImageButton) findViewById(R.id.camera_surface_button); 
         buttonPicture.setOnClickListener(new OnClickListener(){ 
           public void onClick(View v) { 
             mCamera.takePicture(null, null, jpegCallback); 
             //File file = new File(savedPath, "ing.jpg"); 
             sendBroadcast(new   Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" +  Environment.getExternalStorageDirectory()))); 

           } 
         }); 
    . 
    . 
    . 
    . 

    PictureCallback jpegCallback = new PictureCallback() { 
       public void onPictureTaken(byte[] _data, Camera _camera) { 
         imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/TTRprova"); 
         imagesFolder.mkdirs(); 
         String fileName = "image3.jpg"; 

         File output = new File(imagesFolder, fileName); 

         textView1.setText("-----Sono nella callback-----"); 

         Uri outputFileUri = Uri.fromFile(output); 
         String filePath = outputFileUri.getPath(); 
         File file= new File(filePath); 

         try { 
          FileOutputStream fos = new FileOutputStream(file, true); 
          fos.write(_data); 
          fos.close(); 

         } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
          textView1.setText("-----FileNotFoundException-----"); 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          textView1.setText("-----IOException-----"); 
          e.printStackTrace(); 
         } 

         //riparte la preview della camera 
         //mCamera.startPreview(); 



       } 
    }; 

希望在您的幫助。 再見

+0

你使用的是什麼設備? – FoamyGuy 2013-04-05 15:21:18

+0

這個問題肯定是重複的,這個問題已經被問了很多次了。所以我相信你發佈的這一行將是正確的答案。您的問題可能與您的清單相關,缺少許可或缺失活動/廣播標記或類似的內容。你是在模擬器還是物理設備上運行它? – Tobrun 2013-04-05 15:23:02

+0

檢查此問題http://stackoverflow.com/questions/13270789/how-to-run-media-scanner-in-android – Numair 2013-04-05 15:32:27

回答

7

您可以強制MediaScanner添加特定文件(或讓它知道它已被刪除或修改)

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile))); 
相關問題