如何在Android中从Gallery中删除文件?

如何在Android中从Gallery中删除文件?,android,image,android-contentprovider,android-file,delete-file,Android,Image,Android Contentprovider,Android File,Delete File,我正在制作一个应用程序,它具有允许将图像上传到web服务器的功能。将捕获的图像上载到服务器后,图像将从用户设备中删除。我可以上传图像,在我检查文件管理器后,它已经被删除。但不知何故,当我打开图库时,它仍然在那里,尽管它显示“无法加载文件”。但当我检查细节时,它会显示图像的大小。那么,我怎样才能从文件管理器和图库中完全删除上传的图像呢 这是我的代码: Public Static Boolean deleteDirectory(File path) { // TODO Auto-ge

我正在制作一个应用程序,它具有允许将图像上传到web服务器的功能。将捕获的图像上载到服务器后,图像将从用户设备中删除。我可以上传图像,在我检查文件管理器后,它已经被删除。但不知何故,当我打开图库时,它仍然在那里,尽管它显示“无法加载文件”。但当我检查细节时,它会显示图像的大小。那么,我怎样才能从文件管理器和图库中完全删除上传的图像呢

这是我的代码:

Public Static Boolean deleteDirectory(File path) {
        // TODO Auto-generated method stub
        if( path.exists() ) {
            File files = path;

                if(files.isDirectory()) {
                    deleteDirectory(files);
                }
                else {
                    files.delete();
                }
            }

        return(path.delete());
    }
您必须更新(扫描)您的媒体内容提供商。我使用此方法来删除和扫描媒体内容,但您必须进行一些更改

private void DeleteAndScanFile(final Context context, String path,
        final File fi) {
    String fpath = path.substring(path.lastIndexOf("/") + 1);
    Log.i("fpath", fpath);
    try {
        MediaScannerConnection.scanFile(context, new String[] { Environment
                .getExternalStorageDirectory().toString()
                + "/images/"
                + fpath.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        if (uri != null) {
                            context.getContentResolver().delete(uri, null,
                                    null);
                        }
                        fi.delete();
                        System.out.println("file Deleted :" + fi.getPath());
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

请试试这个方法,这个代码对我有用

refreshGallery(this, videoPath);


此代码的可能副本对我有效,此代码有助于删除和更新媒体内容提供商
public static void refreshGallery(Context context, String path) {
    File file = new File(path);
    if (file.exists() && file.isFile()) {
        file.delete();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent intent= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    } else {
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(file.getAbsolutePath())));
    }
}