2014-02-17 3 views

답변

4

아래에 내가 안드로이드 갤러리에서 볼 수있는 모든 이미지를주고 나를 위해 작동 대부분의 장치에서 잠시 기다리면 새로운 사진이 자동으로 감지됩니다. 당신은 갤러리에 즉시 새로 고침을 미리 형성하려면

, 당신은 MediaScanner 클래스를 사용할 필요는 갤러리를 새로 고쳐집니다 - ... 삭제 된 사진을 제거, 등등 새로 추가하고

public void refreshGallery() { 
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image 
    File file = new File(newPhotoPath); 
    Uri contentUri = Uri.fromFile(file); 
    scanIntent.setData(contentUri); 
    sendBroadcast(scanIntent); 
} 

희망이 도움이!

+0

변경'ACTION_MEDIA_MOUNTED'. 답을 수정하여 정답으로 받아 들여주세요! –

+0

완료, 감사합니다! 나는 뭔가 새로운 것을 배웠다 :-) – gilonm

+0

이 오래된 안드로이드에 대한이 작동합니까? 나는 다른 안드로이드 4.4 핸드셋에서 같은 코드가 작동하는 동안 하나의 파일을 스캔하지 않는 안드로이드 4.2 타블렛이 있습니다. android-4.2를 사용하면 전체 sdcard를 스캔 할 수 있지만 하나의 파일은 스캔 할 수 없습니다. 이건 안드로이드 4.2 문제일까요, 내 a-4.2 태블릿 문제일까요? – k3b

0

확인은 내 코드와 그것이 바로이 라인

getallimages(Environment.getExternalStorageDirectory()); 

에서이 함수를 호출하고 제 기능을

private void getallimages(File dir) 
    { 

       String[] STAR = { "*" }; 

     final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER; 
     Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy); 
     int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); 
     int count = imagecursor.getCount(); 
     for (int i = 0; i < count; i++) { 
      imagecursor.moveToPosition(i); 
      int id = imagecursor.getInt(image_column_index); 
      ImageItem imageItem = new ImageItem(); 

      if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760) 
      { 
       imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA)); 

        imageItem.id = id; 
        imageItem.selection = false; //newly added item will be selected by default 
        controller.images.add(imageItem); 

      } 
} 

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

'저장'코드 뒤에이 코드를 삽입하십시오.

이렇게하면 미디어 검사가 시작되고 모든 폴더 ('.nomedia'파일 제외)의 모든 미디어 파일은 갤러리에 표시되는 &으로 업데이트됩니다.

Source.

MediaScanner Documentation.

또는

// Tell the media scanner about the new file so that it is 
// immediately available to the user. 
MediaScannerConnection.scanFile(this, 
     new String[] { file.toString() }, null, 
     new MediaScannerConnection.OnScanCompletedListener() { 
    public void onScanCompleted(String path, Uri uri) { 
     Log.i("ExternalStorage", "Scanned " + path + ":"); 
     Log.i("ExternalStorage", "-> uri=" + uri); 
    } 
}); 

Google's Sample Code

.

+0

이것은 엄청난 시간이 소요되는 절차입니다. 하나의 파일 만 추가하고 싶습니다! –

+0

하나의 파일을 추가하는 방법을 모르겠습니다. 전체 파일 시스템을 재검색하거나 안드로이드 시스템이 자동으로 수행 할 때까지 기다려야합니다. –

+0

developers.android.com에서 찾은 다른 방법이 도움이되기를 바랍니다. –

0

MediaScanner에서 특정 파일, 즉 필요에 따라 이미지 파일을 검색하도록 요청할 수 있습니다. 이렇게하면 MediaScanner에 모든 파일을 검색하도록 요청하는 것보다 적은 오버 헤드가 발생합니다. `ACTION_MEDIA_SCANNER_SCAN_FILE` 해결 문제와

SO: how to run media scanner in android

관련 문제