Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从内部存储器中删除文件_Android_File Io - Fatal编程技术网

Android 从内部存储器中删除文件

Android 从内部存储器中删除文件,android,file-io,Android,File Io,我正在尝试删除存储在内部存储器中的图像。到目前为止,我已经想到了这一点: File dir = getFilesDir(); File file = new File(dir, id+".jpg"); boolean deleted = file.delete(); 这是另一个问题,是: 我的例子总是返回false。我可以在eclipse中的DDMS中看到文件fx2930.jpg。文件getfiledir()。 使用返回整个路径和文件名的方法得到所需的结果。代码如下: File file =

我正在尝试删除存储在内部存储器中的图像。到目前为止,我已经想到了这一点:

File dir = getFilesDir();
File file = new File(dir, id+".jpg");
boolean deleted = file.delete();
这是另一个问题,是:

我的例子总是返回false。我可以在eclipse中的DDMS中看到文件
fx2930.jpg

文件
getfiledir()。
使用返回整个路径和文件名的方法得到所需的结果。代码如下:

File file = new File(inputHandle.getImgPath(id));
boolean deleted = file.delete();

您试过了吗?

您还可以使用:
file.getCanonicalFile().delete()

您是否尝试过
getFilesDir().getAbsolutePath()

似乎通过使用完整路径初始化文件对象解决了问题。我相信这也会奏效

String dir = getFilesDir().getAbsolutePath();
File f0 = new File(dir, "myFile");
boolean d0 = f0.delete(); 
Log.w("Delete Check", "File deleted: " + dir + "/myFile " + d0);
代码
File dir=getFilesDir()无效,因为这是对文件对象的请求。
您正在尝试检索声明目录路径的字符串,因此需要将“dir”声明为字符串,然后请求有权访问该信息的构造函数以字符串形式返回目录的绝对路径。

这对我来说很有效:

爪哇

科特林

val file = File(photoPath) 
file.delete()

MediaScannerConnection.scanFile(context, arrayOf(file.toString()),
      arrayOf(file.getName()), null)

这是一个老话题,但我会补充我的经验,也许有人会觉得这很有帮助

>     2019-11-12 20:05:50.178 27764-27764/com.strba.myapplicationx I/File: /storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/JPEG_20191112_200550_4444350520538787768.jpg//file when it was created

2019-11-12 20:05:58.801 27764-27764/com.strba.myapplicationx I/File: content://com.strba.myapplicationx.fileprovider/my_images/JPEG_20191112_200550_4444350520538787768.jpg //same file when trying to delete it
解决方案1:

              Uri uriDelete=Uri.parse (adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ());//getter getImageuri on my object from adapter that returns String with content uri
这里我初始化内容解析器 并使用传递的URI参数将其删除

            ContentResolver contentResolver = getContentResolver ();
            contentResolver.delete (uriDelete,null ,null );
解决方案2(我这一次从head得到的第一个解决方案我确实知道):内容解析器存在

              String path = "/storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/" +
                    adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ().substring (58);

            File file = new File (path);
            if (file != null) {
                file.delete ();
            }
希望这将有助于某人 快乐编码

File file = new File(getFilePath(imageUri.getValue())); 
boolean b = file.delete();
在我的情况下不起作用

boolean b = file.delete();                 // returns false
boolean b = file.getAbsolutePath.delete(); // returns false 
总是返回false

该问题已通过使用以下代码解决:

ContentResolver contentResolver = getContentResolver();
contentResolver.delete(uriDelete, null, null);

尝试使用Contex.deletFile()的变体,但该变体无效。下面是似乎有效的方法。@user661543 ih.getImgPath()返回的完整路径是什么?您传递了什么作为删除文件的参数?如果上述方法不起作用,则很可能将您的文件存储在应用程序包之外。或者您可能传递了错误的文件名作为参数。+1这是删除通过
newoutputstreamwriter(context.openFileOutput(fileName,context.MODE_PRIVATE)).write(data)写入的文件的最简单方法其中
inputHandle.getImgPath(id)
是文件路径我知道我不能留下“谢谢”的消息,但是。。。。我太爱你了!!!!!谢谢
boolean b = file.delete();                 // returns false
boolean b = file.getAbsolutePath.delete(); // returns false 
ContentResolver contentResolver = getContentResolver();
contentResolver.delete(uriDelete, null, null);