2013-11-28 72 views
2

如何刷新android kitkat畫廊?在android kitkat刷新畫廊

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

我試過了上面的,但是它在android 4.4中沒有提神。如何以編程方式添加/刪除圖像時刷新圖庫?

回答

0

您可以使用下面的技術更新出現在一個文件夾中的所有文件:

for (File child : fileFolder.listFiles()) { 
    if (child.isFile()) { 
     fName = child.getName(); 
     Log.d("MyTag", "Scanning >> " + child.getName()); 

    MediaScannerConnection 
     .scanFile(MyActivity.this, 
     new String[] { "path/of/our/folder" + fName }, 
     null, new MediaScannerConnection.OnScanCompletedListener() { 
       public void onScanCompleted(
       String path, Uri uri) { 
       Log.i("ExternalStorage", "Scanned " + path + ":"); 
       Log.i("ExternalStorage", "-> uri=" + uri); 
       } 
     }); 
    } 
} 

來源:here

1

這對我的作品:)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File("folderPATH", "fileName"); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    appContext.sendBroadcast(mediaScanIntent); 
} else { 
    appContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + "FOLDER_TO_REFRESH"))); 
} 

希望它能幫助: )

+0

不適用於Android Lollipop 5.0,爲什麼? –

0

使用此代碼添加/刷新圖庫圖像。

String version = Build.VERSION.RELEASE; 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED); 
     String mCurrentPhotoPath = "file://" 
       + Environment.getExternalStorageDirectory() + "/AppDirectory"; // image 
                       // is 
                       // the 
                       // created 
                       // file 
                       // image 
     File file = new File(mCurrentPhotoPath); 
     Uri contentUri = Uri.fromFile(file); 
     mediaScanIntent.setData(contentUri); 
     sendBroadcast(mediaScanIntent); 
    } else { 
     MediaScannerConnection.scanFile(this, new String[] { Environment 
       .getExternalStorageDirectory().toString() }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        /* 
        * (non-Javadoc) 
        * 
        * @see android.media.MediaScannerConnection. 
        * OnScanCompletedListener 
        * #onScanCompleted(java.lang.String, android.net.Uri) 
        */ 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i(TAG, "Scanned ................" + path); 
        } 
       }); 
    }