This is any way to delete only .jpg files from folder?
This is my remove method:
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
How can I remove only .jpg files from folder?
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
String filename = children[i];
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg"))
new File(dir, filename).delete();
}
}
or you prefer the for-each version
if (dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
if (child.endsWith(".jpeg") || child.endsWith(".jpeg"))
new File(dir, child).delete();
}
}
Try like this
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
if(children[i].endsWith('.jpg' || children[i].endsWith('.jpeg'))
{
new File(dir, children[i]).delete();
}
}
}
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
Then call
walkdir(dir);
public void walkdir(File dir) {
String Patternjpg = ".jpg";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(Patternjpg)){
//Do what ever u want
listFile[i].delete();
}
}
}
}
}
Related
I'm making application that find specific file in my storage.
So I put all file list into List.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
File[] files = directory.listFiles();
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].exists()) {
if (files[i].isDirectory()) {
File[] file = files[i].listFiles();
for (int j = 0; j < file.length; j++) {
searchFile(file[j].getPath());
}
} else
fileList.add(files[i].getPath());
}
}
}
} catch(Exception ex){}
}
But My the number of all file is more than 60000.
So When I tried to debug, It worked so slowly.
How can I get All file list in my storage quickly?
Please try the below code.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if(file.exists()) {
if (file.isDirectory()) {
File[] innerFiles = file.listFiles();
for (File innerFile : innerFiles) {
searchFile(innerile.getPath());
}
} else
fileList.add(file.getPath());
}
}
}
} catch(Exception ex){}
}
This will narrow down your performance issue to a certain level.
I have a problem getting the size of a file.
I have the following code:
File file = new File(path);
FilenameFilter mediafilefilter = new FilenameFilter(){
private String[] filter = {".txt"};
#Override
public boolean accept(File dir, String filename) {
for(int i= 0;i< filter.length ; i++){
if(filename.indexOf(filter[i]) != -1)return true;
}
return false;
}
};
File[] flies = file.listFiles(mediafilefilter);
if (files != null) {
{
if (files.length > 0)
{
System.out.println("Totol is :" + files.length);
for (int j = 0; j < files.length; j++) //not work
}
}
}
some text file is 0 byte
Like that
list[0].length()/1024
list[0] is the first file in your array
public long getFileSizes(File f) throws Exception{
long s=0;
if (f.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(f);
s= fis.available();
}
return s;
}
i have a Problem with String[] array.I refer about this from Here
& Here
But still, i have not found any solution from the link.
What I want:
I am developing a Custom Camera App.I have a Folder in which I am saving a Captured images.also, i have 1 ImageView. when my App Launched the Image [or Bitmap] of Folder is set into that ImageView (Always set the First image of the folder into ImageView).
Following is my Code.
private void loadImageInImageView() {
Uri[] mUrls;
String[] mFiles = new String[0];
File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");
File[] imageList = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File file, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
}
});
if (mFiles.length >= 0) {
Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
} else {
mFiles = new String[imageList.length];
for (int i = 0; i < imageList.length; i++) {
mFiles[i] = imageList[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for (int i = 0; i < mFiles.length; i++) {
mUrls[i] = Uri.parse(mFiles[i]);
imgBtnThumbnail.setImageURI(mUrls[i]);
}
}
}
in Above code [when Folder is Empty] :
When i set if (mFiles.length > 0)
it shows me an Error
Error: Attempt to get length of null array
When i set if (mFiles.length >= 0)
Then it shows me same as Above Error.
When i set if (mFiles == null)
it is also not Working because I have initialized String[] in above code.
String[] mFiles = new String[0];
Hence it also not work.
When I have some Images then it working Fine.because it executed the else part.
what should I do?Whenever my Folder is Empty then it Shows me a Toast.otherwise my else code will be executed.
Any help will be Highly appreciated.
File[] imageList = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File file, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
}
});
if (imageList.length = 0) {
Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
} else {
String[] mFiles = new String[imageList.length];
for (int i = 0; i < imageList.length; i++) {
mFiles[i] = imageList[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for (int i = 0; i < mFiles.length; i++) {
mUrls[i] = Uri.parse(mFiles[i]);
imgBtnThumbnail.setImageURI(mUrls[i]);
}
}
If the function you want works when the size of the imageList is greater than zero, try below code.
private void loadImageInImageView() {
File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");
File[] imageList = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File file, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
}
});
// conditional operator [ ? : ]
// value = (experssion) ? value if true : value if false
// ref : https://www.tutorialspoint.com/java/java_basic_operators.htm
int imgLength = imageList == null ? 0 : imageList.length;
if(imgLength > 0)
{
String[] mFiles = new String[imgLength];
Uri[] mUrls = new Uri[imgLength];
//merge for condition
for (int i = 0; i < imgLength; i++) {
mFiles[i] = imageList[i].getAbsolutePath();
mUrls[i] = Uri.parse(mFiles[i]);
imgBtnThumbnail.setImageURI(mUrls[i]);
}
}
else
{
Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
}
}
Do not initialize mFiles. Initialize it in the moment when you add some data.
And for checking if it isn't empty use this:
if (mFiles != null && mFiles.length > 0) {
...
}
private void loadImageInImageView() {
Uri[] mUrls;
File[] imageList
File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");
imageList = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File file, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
}
});
if (imagelist==null && imageList.length == 0) {
Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < imageList.length; i++) {
imgBtnThumbnail.setImageURI(Uri.parse(imageList[i].getAbsolutePath()););
}
}
}
try this;
but your image changes continuously because of for loop;
I cant delete the folder in storage. This is my code
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/sdcard/folder" );
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
This is how you can do it
but first you need to have this permission in menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and then
File dir = new File(Environment.getExternalStorageDirectory()+"/sdcard/folder");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
How can I get only images' names in known directory.
Trying use this (from here)
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
if (f.isFile())
String name = f.getName();
// make something with the name
}
but don't works.
How can I do this?
private ArrayList<File> fileList = new ArrayList<File>();
//getting SDcard root path
File root = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
getfile(root);
for (int i = 0; i < fileList.size(); i++) {
// getting file name
System.out.println(fileList.get(i).getName());
if (fileList.get(i).isDirectory()) {
teSystem.out.println("This is Directory not an image");
}
}
// getfile(file) method
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if (listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
hope it will help your purpose.