android - 如何从 Android 中的图库中删除文件?

标签 android image android-contentprovider android-file delete-file

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

这是我的代码:

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();
    }

}

关于android - 如何从 Android 中的图库中删除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932434/

相关文章:

android - 多个应用程序使用相同的内容提供商

android - GearVR 覆盖后退和主页按钮

android - Drupal Android 应用程序登录持久性

android - 在上下文操作中使用导航会引发异常

ic_sync_black_24dp.xml 的 android.content.res.Resources$NotFoundException

javascript - 预览图像 HTML 和 JavaScript

css - 根据分辨率自动调整背景图像大小

android - 在Android中使用ContactsContract时出现"SQLiteException: no such column: mimetype"

html - 根据标题宽度过渡

android - 实现android content provider的查询方法,使用多个表