display all files from specific folder from sdcard Android - android

how can i display all the files (.pdf, .doc, .ppt, .mp4, etc...) inside a folder in sdcard? i want to display their filenames.
here's what i've got:
File files = new File(Environment.getExternalStorageDirectory() + "/Downloads/Files/");
File[] list = files.listFiles();
int lists = files.listFiles().length;
String qwe = list.toString();
Log.d(LOG_TAG, "list: " + lists);
Log.d(LOG_TAG, "names: " + qwe + "\n"); //i want to show their filenames
for(int i=0; i < questionfiles.listFiles().length; i++){
if(list[i].isHidden()){
Log.d(LOG_TAG, "hidden path files.."+list[i].getAbsolutePath());
}
}

You have to use a ListActivity with an Adapter (ArrayAdapter for istance).
see this as example
EDIT: You can print the array content in this way:
Log.i(LOG_TAG, Arrays.toString(list));

Related

How to check pdf files from mobile storage and list the name by consensus with dynamic list view

I have dynamic listView linked with JSONArray, in the adapter i'm trying to read the storage to check if the pdf file is downloaded already the Button text will be View and will call View method but if the the file not there the Button text will be Download and will call Download method.
I have been trying this code but it's not work
String path2 = Environment.getExternalStorageDirectory() + "/materialpdffolder/";
File path = new File(path2);
File list[] = path.listFiles();
for( int i=0; i < materialList.size() ; i++)
{
for( int x=0; x <= list.length ; x++) {
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
}
The problem is when there’s no file, the application is stack and give error and not give any results for list[x].get Name() and when I use materiallist.size() for for looping it is doesn’tgive a correct answer
Try to change with this code
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
Remove one bracket from }}else and put it end of else

Get all folders with photo - Android

String albumName = albumTitle;
String ExternalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/My_Albums/" + albumName;
File targetDirectory = new File(ExternalStorageDirectory);
if (targetDirectory != null) {
File[] files = targetDirectory.listFiles();
if (files != null) {
filePathString = new String[files.length];
fileNameString = new String[files.length];
for (int i = 0; i < files.length; i++) {
filePathString[i] = files[i].getAbsolutePath();
System.out.println("filePAthStr: " + filePathString[i]);
fileNameString[i] = files[i].getName();
System.out.println("fileNameStr: " + fileNameString[i]);
customGridAdapter.add(filePathString[i]);
}
}
}
How do I get all folders with photo.
How do I get media from mediastore efficiently
Intially , album name will be empty
String albumName = albumTitle;
So if you aint enter the album name, the path would be like
String ExternalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/My_Albums/";
it will go the parent path ,get all the albums under that node and display the number of images under it.
when once you provide albumname,it will get all images inside it and displays.In this scenario you need to loop inside album to cover each and every album under it and get the image paths from them.
UPDATE
For implementing this Logic refer link : Get All Folders With Photos
I got the solution, which was quite simple.
if(albumName != null && !albumName.equals("")){
// Only then set the adapter
}

print ftp files in list view

I m developing a app that downloads list of files from ftp server. the file names are downloaded using array. how can i pass this array to main activity and display it in a list view.i have a very little knowledge in this domain so kind of help is appreciated.thanking you in advance
FTPFile[] ftpFiles = mFTPClient.listFiles("/public_html/");
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
You need to use ListView widget. The simplest way of displaying a list of items in a list view would be to use predefined ArrayAdapter class and an existing Android layout for the rows. Please check this tutorial for detailed explanation how to use ListView.
http://www.vogella.com/articles/AndroidListView/article.html

comparing between two arraylists and remove extra element from there

how do I compare two arraylists?
I have a sdCoverList array and thumbnailList array.
02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549]
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028]
In my sd cover list, i have an extra value that i want to remove away but first i'd have to compare with thumbnailList array first. if both have the same elements, don't do anything but if theres an extra, remove it from sd card.
My array lists
for sdCoverList:
// CHECK DIRECTORY BEFORE DELETING THEM
File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
File yourDir = new File(strPath, "");
if(yourDir.length()!=0)
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
sdCoverList.add(name);
//System.out.println(sdCoverList);
}
for hashedthumbnail
for (int a=0;a<=thumbnailList.size()-1;a++)
{
String hashed = String.valueOf(thumbnailList.get(a).hashCode());
Log.d("hashed",hashed);
hashedThumbnail.add(hashed);
}
List<String> sdCoverList = new ArrayList<String>();
sdCoverList.add("-2122410790");
sdCoverList.add("2043473787");
sdCoverList.add("717409028");
List<String> hashedThumbnail = new ArrayList<String>();
hashedThumbnail.add("-2122410790");
hashedThumbnail.add("2043473787");
System.out.println("sdCover List: " + sdCoverList);
System.out.println("hashedThumbnail List: " + hashedThumbnail);
List<String> temp = new ArrayList<String>();
temp.addAll(sdCoverList);
temp.removeAll(hashedThumbnail);
System.out.println("temp List: " + temp);
sdCoverList.removeAll(temp);
System.out.println("sdCover List: " + sdCoverList);
The output will be
sdCover List: [-2122410790, 2043473787, 717409028]
hashedThumbnail List: [-2122410790, 2043473787]
temp List: [717409028]
sdCover List: [-2122410790, 2043473787]

android downloading and naming mp3 file

I am downloading mp3 file from internet and storing them into my SDCARD i am giving that mp3 file name as a server has given i.e if i am downloading test.mp3 file then i am giving name it to test.mp3 and storing into SDCARD
But now my requirement is to giving them name as 1 2 3 like this..
how can i do this
currently i am doing using substring function like this
GlobalVariable.Setstrpath(temp1[0].toString().substring(
temp1[0].toString().length() - 15,// 15
temp1[0].toString().length()))
i am saving them into SDCARD/DOWNLOAD folder
how it can be possible?
Log.i("TEST 1 ", Ringtones.size() + "");
if (Ringtones.size() == 0) {
GlobalVariable.SetCounter(1);
} else {
GlobalVariable.SetCounter(Ringtones.size() + 1);
}
GlobalVariable.Setstrpath(GlobalVariable.GetCounter() + ".mp3");
Right now i am using this solutionplease suggest me if any best solution if available thanks Pragna
i find another solution also
for (int i = 0; i < Ringtones.size(); i++) {
String s = Ringtones.get(i).toString();
Log.i("TEST", s.toString());
String str = s.toString().substring(0, s.indexOf(".mp3"));
System.out.println(Integer.parseInt(str));
int cntr = Integer.parseInt(str);
System.out.println("TEST " + cntr);
System.out.println(cntr + 1);
GlobalVariable.SetCounter(cntr + 1);
GlobalVariable.Setstrpath(GlobalVariable.GetCounter() + ".mp3");
}
still better solution is acceptable

Categories

Resources