从图库或照片中删除图片 android 8.0

我想根据其 URI 从设备中删除图库或照片应用程序中的图片。我在互联网上尝试了几种方法,但没有找到。


我调用了下面的方法


deleteMethod(getPath(selectedImageUri));

这两个方法定义在这里。


private void deleteMethod(String file_dj_path) {

    File fdelete = new File(file_dj_path);

    if (fdelete.exists()) {

        if (fdelete.delete()) {

            System.out.println("file Deleted :" + file_dj_path);

            Toast.makeText(getApplicationContext(),

                    "file Deleted", Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(getApplicationContext(),

                    "file not Deleted", Toast.LENGTH_SHORT).show();

            System.out.println("file not Deleted :" + file_dj_path);

        }

    }

}


public String getPath(Uri uri) {

    String[] projection = {MediaStore.Images.Media.DATA};

    Cursor cursor = managedQuery(uri, projection, null, null, null);

    if (cursor != null) {

        //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL

        //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA

        int column_index = cursor

                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        return cursor.getString(column_index);

    } else return null;

}

我在清单中添加权限。像,


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我的提供者看起来像:


    <paths xmlns:android="http://schemas.android.com/apk/res/android">


    <external-path path="Android/data/com.***.calculator/"

        name="files_root" />

    <external-path path="." name="external_storage_root" />



</paths>

我有这样的uri:


:/storage/emulated/0/DCIM/Camera/IMG_20180804_181447.jpg

我错过了什么吗?


ABOUTYOU
浏览 259回答 3
3回答

慕的地6264312

改成这样&nbsp;String path = selectedImageUri.getPath()&nbsp;&nbsp;deleteMethod(path);然后里面的delete方法File file = new File(new URI(path));if (fdelete.exists()) {if (fdelete.delete()) {&nbsp; &nbsp; System.out.println("file Deleted :" + uri.getPath());} else {&nbsp; &nbsp; System.out.println("file not Deleted :" + uri.getPath());}&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java