Reputation: 42824
I Created a Image at the location. And I can see The Image got created
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;
}
How to delete all the images in that directory ?
I tried with:
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()
is failing and saying its not a directoryHow to Delete images properly ?
Upvotes: 2
Views: 85
Reputation: 157447
In your delete code you should be using
File dir = Environment.getExternalStoragePublicDirectory(mDirectoryPath)
And not
File dir = new File(mDirectoryPath);
.
In your code the file will point to "/Pictures/MyDirectoryName"; which doesn't exist (your app doesn't have permission to write on the root /)
Upvotes: 1