如何删除使用 Uri 创建的文件?

2024-03-22

我的应用程序从相机拍摄照片并将其保存在一个文件中,该文件的 Uri 存储在 SQL 数据库中。使用数据库中的 Uri 初始化位图可以完美地工作。但是,当我尝试使用数据库中的 Uri 初始化文件,然后使用删除时imagefile.delete()这是行不通的。我尝试了其他帖子中给出的几种方法,但没有奏效。

这就是我保存文件的方式。

相机意图:

Intent startCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (startCamera.resolveActivity(getPackageManager())!=null)
                    {
                        photoFile= null;
                        try{
                            photoFile = createImageFile();
                        }
                        catch (IOException ex)
                        {
                            Log.e("File Creation","error");
                        }
                        if (photoFile!=null)
                        {
                            photoURI = FileProvider.getUriForFile(context,"lcukerd.com.android.fileprovider",photoFile);
                            startCamera.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                            camerastarted=true;
                            startActivityForResult(startCamera,CAMERA_REQUEST);
                        }
                    }

声明方法:

private File createImageFile() throws IOException
{
    String EName = "Stuff";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(EName,".jpg",storageDir);
    return image;
}

压缩图像:

photoFile.delete();
FileOutputStream out = null;
                    try {
                        File image = createImageFile();
                        photoURI = FileProvider.getUriForFile(context, "lcukerd.com.android.fileprovider", image);
                        out = new FileOutputStream(image);
                        photo.compress(Bitmap.CompressFormat.PNG, 100, out);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) {
                                out.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

将 Uri 保存到数据库是将 Uri 简单转换为字符串,然后保存在表的 TEXT 列中。

帮我删除图像文件。

Edit:

我尝试跟随。

private void deleteimage(String imageloc)
    {
        File imagefile = new File(getRealPathFromURI(Uri.parse(imageloc)));
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }
    public String getRealPathFromURI( Uri contentUri) {
        String result;
        Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentUri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

and..

private void deleteimage(String imageloc)
    {
        File imagefile = new File(Uri.parse(imageloc).getpath());
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }

没有一个起作用。


由于这是一个Uri你从中得到的FileProvider, call delete() on a ContentResolver,传入Uri (plus null对于其余参数)。这将删除底层文件,至少根据the FileProvider文档 https://developer.android.com/reference/android/support/v4/content/FileProvider.html#delete(android.net.Uri,%20java.lang.String,%20java.lang.String%5B%5D).

或者,存储文件路径,而不是Uri,在您的数据库中,因为这是您的文件。您可以重新创建Uri稍后根据需要使用FileProvider.getUriForFile(),你可以使用delete() on File删除文件(给定路径)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何删除使用 Uri 创建的文件? 的相关文章

随机推荐