作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在该位置创建了一个图像。我可以看到图像已创建
String Dirlocation = "Pictures/MyDirectoryName";
String mImageName = System.currentTimeMillis()+".jpg";
createFile(mDirectoryPath,mImageName,fileData);
private String createFile(String mDirectoryPath, String mImageName, byte[] fileData) throws IOException {
FileOutputStream out=null;
try{
File root = Environment.getExternalStoragePublicDirectory(mDirectoryPath);
File dir = new File(root + File.separator);
if (!dir.exists()) dir.mkdir();
//Create file..
String mFinalUri = root + File.separator + mImageName;
File file = new File(mFinalUri);
file.createNewFile();
out = new FileOutputStream(file);
if(out!=null){
out.write(fileData);
MediaScannerConnection.scanFile(context, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);
}
//Check if file exists if true return the URI
File mFile = new File(mFinalUri);
if(mFile.exists()){
return mFinalUri;
}else{
return null;
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
out.close();
}
return mDirectoryPath;
}
如何删除该目录下的所有图片?
我试过:
String mDirectoryPath = "Pictures/MyDirectoryName";
File dir = new File(mDirectoryPath);
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
dir.isDirectory()
失败并表示它不是目录如何正确删除图片?
最佳答案
在您应该使用的删除代码中
文件目录 = Environment.getExternalStoragePublicDirectory(mDirectoryPath)
而不是
File dir = new File(mDirectoryPath);
。
在您的代码中,文件将指向“/Pictures/MyDirectoryName”;不存在(您的应用没有在根/上写入的权限)
关于android - 如何从android中的目录中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141639/
我是一名优秀的程序员,十分优秀!