2013-11-28 73 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); 
        } 
       }); 
    }