This is how i tried to access files from SDcard
// Use the current directory as title
path = "/sdcard/";
if (getIntent().hasExtra("path")) {
path = getIntent().getStringExtra("path");
}
setTitle(path);
// Read all files sorted into the values-array
final List values = new ArrayList();
File dir = new File(path);
if (!dir.canRead()) {
setTitle(getTitle() + " (inaccessible)");
}
final String[] list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.startsWith(".")) {
values.add(file);
}
}
}
Collections.sort(values);
but not getting how to display only 3Gp vales in list ,anyone suggest me to how to do it.
Thank you.
Have you tried just checking to see if the file has a .3gp extention?
Like:
if(file.contains(".3gp"))?
Related
I am currently working with internal storage
i have some problems,
I am also get the files in particular folder on internal storage,but my list shows all root folders like data/data/com.example.app/sample.mp4, But i want only filename like sample.mp4.
Here my code
`
ArrayList<File> fileList = new ArrayList<File>();
File mydir = this.getDir("Myfolder", Context.MODE_PRIVATE);
File listFile[] = mydir.listFiles();
if (listFile != null && listFile.length > 0) {
for (File aListFile : listFile) {
if (aListFile.isDirectory()) {
fileList.add(aListFile);
} else {
if (aListFile.isFile());
{
fileList.add(aListFile);
}
}
}`
Get filename from file object
if (aListFile.isFile());
{
string fileName = aListFile.getName();
}
try this one,
YourFilePath.substring(YourFilePath.lastIndexOf("/")+1)
For example,
String filePath = "data/data/com.example.app/sample.mp4"
String fileName = filePath.subString(filePath.lastIndexOf("/")+1);
And finally you get your fileName = sample.mp4
I am building an Android app that writes files in the format of my custom .fire extension.
I need to access them all at once and put them in a ListView. Could someone please help me to fetch them via an activity or Async Task Manager?
You can write an Async Task to fetch the .fire files from the folder into which you are writing them initially and dynamically add the filenames into the listview.
You can do something like this in your async task:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/"+FolderWhichContainsyourFiles);
File[] listOfFiles = dir.listFiles();
for (int count = 0; count < listOfFiles.length; count++) {
File file = listOfFiles[count];
if (file.isFile() && file.getName().endsWith(".fire")) {
String fileName = file.getName();
//Here you can dynamically add this name into the ListView
}
}
}
You need to use an ArrayAdapter and add elements to the ArrayAdapter dynamically. It is brilliantly explained here: Dynamically add elements to a listView Android
if you want to find files recursively:
private static List<File> filesList = new ArrayList<File>();
public static List<File> recursiveList(String filePath) {
try {
File[] dir = new File(filePath).listFiles();
for (File item : dir) {
if (item.isDirectory()) recursiveList(item.getAbsolutePath());
else if (item.isFile() && item.getName().endsWith(".fire"))
filesList.add(item);
}
return filesList;
} catch (Exception e) {
return null;
}
}
and to retrieve files in activity:
List<File> files = recursiveList("/sdcard/Music/");
for (File file : files)
adapterFilesList.add("" + file.getName());
I am trying to program my own file manager for android. What I want to know is that how can I read the file names of all the files present in external/internal storage?
I want to read the file names and show them in a listview so the user could see what files are present in what folder. I know this thing is going to work in recursive manner as I have to read the content of sub directories as well.
This is what you have to do. Before going to write please refer the File class in java. This will help you to clear lot of things.
Below is the snippet that provides the list of files.
File directory = new File(path);
File[] listFiles = directory.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory())
// do the stuff what you need
else if (file.isFile()) {
// do the stuff what you need
}
}
}
}
Following code will give you list of files in android sdcard:
/**
* Return list of files from path. <FileName, FilePath>
*
* #param path - The path to directory with images
* #return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"
*/
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "bmp", "gif");
#Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
You can call same method to get sub directory file also.
Im trying to create my own activity for that im using reference of :
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
so in this code he have explained only how to get files from SD card.
Instead i want to access them from either android assets or drawable resources.
following is the code of function which returning images filepath from sd card.
From Util.java
// Reading file paths from SDCard
public ArrayList<String> getFilePaths() {
ArrayList<String> filePaths = new ArrayList<String>();
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + AppConstant.PHOTO_ALBUM);
// check for directory
if (directory.isDirectory()) {
// getting list of file paths
File[] listFiles = directory.listFiles();
// Check for count
if (listFiles.length > 0) {
// loop through all files
for (int i = 0; i < listFiles.length; i++) {
// get file path
String filePath = listFiles[i].getAbsolutePath();
// check for supported file extension
if (IsSupportedFile(filePath)) {
// Add image path to array list
filePaths.add(filePath);
}
}
} else {
// image directory is empty
Toast.makeText(
_context,
AppConstant.PHOTO_ALBUM
+ " is empty. Please load some images in it !",
Toast.LENGTH_LONG).show();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(_context);
alert.setTitle("Error!");
alert.setMessage(AppConstant.PHOTO_ALBUM
+ " directory path is not valid! Please set the image directory name AppConstant.java class");
alert.setPositiveButton("OK", null);
alert.show();
}
return filePaths;
}
Take a look at the AssetManager class:
context.getAssets() and then use list("") to get a list of the assets in your application.
See http://developer.android.com/reference/android/content/res/AssetManager.html#list(java.lang.String)
This question already has an answer here:
Find all files on sd card
(1 answer)
Closed 8 years ago.
I want to get file names stored in sd card android, but i could not find a proper solution.
Can somebody please help me??
public GetFileNames(Context context) {
// TODO Auto-generated constructor stub
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard, "path");
String name;
GenerateAlertBox alert = new GenerateAlertBox();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
// do whatever you want with filename
alert.GenerateAlertBoxes(name, context);
}
}
}
I found this,but I don't the the path.
If you want to find all the files in the top folder of the SDCard you can do something like this:
public GetFileNames(Context context) {
File dir = Environment.getExternalStorageDirectory();
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
fileNames.add(name);
}
}
//Do whatever you want with fileNames array.
}
EDIT: If you want to get your apps private filenames you should do this:
public GetFileNames(Context context) {
File dir = context.getFilesDir();
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
fileNames.add(name);
}
}
//Do whatever you want with fileNames array.
}