画廊刷新在android中

时间:2013-12-18 09:22:18

标签: android gallery

我正在我的应用程序中删除Gallery中的文件。该文件已成功删除但删除后文件仍显示在Gallery中,直到我重新启动设备。 我尝试了不同的解决方案,如

       Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
               Uri.fromFile(from.getParentFile()));
          sendBroadcast(intent);

和媒体扫描仪

public class SingalmediaScaner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingalmediaScaner(Context context, File f) {
    mFile = f;
    mMs = new MediaScannerConnection(context, this);
    mMs.connect();
}

@Override
public void onMediaScannerConnected() {
    mMs.scanFile(mFile.toString(), null);
}

@Override
public void onScanCompleted(String path, Uri uri) {
    mMs.disconnect();
}

private static void removeThumbnails(ContentResolver contentResolver,
        long photoId) {
    Cursor thumbnails = contentResolver.query(
            Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID
                    + "=?", new String[] { String.valueOf(photoId) }, null);
    for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails
            .moveToNext()) {

        long thumbnailId = thumbnails.getLong(thumbnails
            .getColumnIndex(Thumbnails._ID));
    String path = thumbnails.getString(thumbnails
            .getColumnIndex(Thumbnails.DATA));
    File file = new File(path);
    if (file.delete()) {

        contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI,
                Thumbnails._ID + "=?",
                new String[] { String.valueOf(thumbnailId) });

    }

}
}

}

但以上都不适用于我 该文件已删除,但图库未刷新。 如何刷新画廊而不重新启动我的设备。 或如何在不重新启动我的应用程序的情况下刷新图库

4 个答案:

答案 0 :(得分:2)

因为你的图像存储在存储卡中,你需要刷新存储卡才能看到反映的变化。

使用:  sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(“file://”)                             + Environment.getExternalStorageDirectory())));

答案 1 :(得分:1)

我使用了以下方法。

private void deleteFormGallery(String filepath){
        String[] retCol = { MediaStore.Video.Media._ID };
        Cursor cur = application.getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, retCol, 
            MediaStore.MediaColumns.DATA + "='" + filepath + "'", null, null);

        Log.i(TAG, "::deleteFormGallery: count = " + cur.getCount());
        if (cur.getCount() == 0) {
            return;
        }
        cur.moveToFirst();
        int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));       
        cur.close();

        Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
        int rows = application.getContentResolver().delete(uri, null, null);        
        Log.i(TAG, "::deleteFormGallery: " + id + " deleted rows = "+rows);
    }

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

我使用以下代码(这也非常类似于我创建视频的代码,该代码也告诉媒体系统有关文件更改并正确更新图库):

private void deleteVideo(String videoUrl)
{
    File videoFile = new File(videoUrl);
    if (!videoFile.delete())
    {
        Log.e(TAG, "Failed to delete " + videoUrl);
    }
    else
    {
        MediaScannerConnection.scanFile(mContext,new String[] { videoUrl }, null, new MediaScannerConnection.OnScanCompletedListener() 
        {
            public void onScanCompleted(String path, Uri uri) 
            {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    }
}