在android kitkat刷新画廊

时间:2013-11-28 11:50:13

标签: android

如何在android kitkat中刷新图库?

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

我试过上面的内容,但它在android 4.4中并不令人耳目一新。以编程方式添加/删除图像时如何刷新图库?

3 个答案:

答案 0 :(得分: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")));
}

希望有所帮助:)

答案 1 :(得分: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

答案 2 :(得分: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);
                    }
                });
    }