Delete copy of an image in sdcard

后端 未结 3 1246
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 00:38

Hello friends I am using the following code in my my project.

PERMISSIONS:



        
相关标签:
3条回答
  • 2020-12-22 01:12

    This is the complete solution of your problem of deleting the file from dcim folder.

    just copy and paste this method. And call it whenever necessary.
    private void deleteLatest() {
            // TODO Auto-generated method stub
            File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera" );
    
            //Log.i("Log", "file name in delete folder :  "+f.toString());
            File [] files = f.listFiles();
    
            //Log.i("Log", "List of files is: " +files.toString());
            Arrays.sort( files, new Comparator<Object>()
                    {
                public int compare(Object o1, Object o2) {
    
                    if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                        //         Log.i("Log", "Going -1");
                        return -1;
                    } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                        //     Log.i("Log", "Going +1");
                        return 1;
                    } else {
                        //     Log.i("Log", "Going 0");
                        return 0;
                    }
                }
    
                    });
    
            //Log.i("Log", "Count of the FILES AFTER DELETING ::"+files[0].length());
            files[0].delete();
    
        }
    
    0 讨论(0)
  • 2020-12-22 01:18

    As in the thread @vipsy linked, you can specify the file path URI with

    File fileDir = new File(Environment.getExternalStorageDirectory() +
        "/saved_images");
    fileDir.mkdirs();
    
    File file = new File(fileDir, "image.jpg");
    
    Uri outputFileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    

    This way the camera will save the image to the specified path instead of the DCIM folder. You don't have to manually copy it, nor do you have to delete anything.

    EDIT: You have to create the folder on the sdcard beforehand, maybe that's the poblem. Otherwise, this should work.

    0 讨论(0)
  • 2020-12-22 01:22
       File fileOrDirectory= new File(Environment.getExternalStorageDirectory() +
        "/saved_images");
    
    here fileOrDirectory is a path where you are saving images captured by camera .
    you can call this method passing file directory when u need;
    
    
    
     void DeleteRecursive(File fileOrDirectory) {
            if (fileOrDirectory.isDirectory())
                for (File child : fileOrDirectory.listFiles())
                    DeleteRecursive(child);
    
            fileOrDirectory.delete();
        }
    
    0 讨论(0)
提交回复
热议问题