Displaying only names of files in listview - android

In my app am creating some files and storing them in cache dir. now from app only I want to access this files so i get them in a ListView by giving the path getExternalCacheDir().getAbsolutePath(). Here, I get a ListView with the whole path of the files. Instead I just want the file name to be displayed in list.
Can someone help?

Simply call list() in the diretory to return an array of the file names in that directory:
File directory = getExternalCacheDir();
String[] filenames = directory.list();
Alternatively, you can call listFiles() to get back a File[] if that if more useful to you.
HTH

Related

Show all .txt files from a folder in an activity

I have a application where the user can write a text into a EditText and the written text is saved in an auto generated .txt file.
Now I want to show all the txt files from the folder in an activity, not like a regular file browser, just the title of the .txt files in a ListView or as a TextView (each file is illustrated as a TextView).
Get the path of that directory as string and create a new file with this path then,
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder";
File file=new File(path);
or
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder");
Use file.list which will give u all files name in form of an array then use a for loop with endsWith function and store data into a list like this
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
if(i.endsWith(".txt")){
l.add(i);
}
}
you will have names all text files under selected directory in list l
In Java 8 you can use Files API and Stream API
Files
.list(Paths.get("."))
.forEach(System.out::println);

Adding all images from a folder into an array

I'm wanting to add a large amount of images to an image array and I'm wondering if there is a way to add all the images in a specified folder rather than adding all of them manually in big list.
As far as I know what you want this should make a job:
File folder = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/folderWithImages";
File list[] = file.listFiles();
This will give you array of files in directory. Only I dont remember that adding slash is necessary.

Retrieve new and old path of a renamed file under observing directory

My App saves recorded video files in a specific folder in external directory and saves meta data including the path of every file in database . I need to Observe remove , rename and move of my file in case of keeping database updated with new file path. I read about FileObserver in android. If I give the path of my directory in SD Card for example /storage/emulated/0/my_recordings as input path of my FileObserver, events will be fired for all of my files in the input path and also it will be fired for MOVED_TO event type which i use to observe rename operation of files under my directory, I will get the new relative path of my file in the path argument of onEvent (int event, String path) but how can I retrieve the old file name or old file path of that file to understand which database entry I have to update ? I passed the path of parent directory of that file so I don't have the old file name.
Thanks in advance
There should be a MOVED_FROM event right before the MOVE_TO. The path of the MOVED_FROM event should be the old file name while the path in the MOVED_TO should contain the new file name.
Also make sure that you monitor this event type by explicitly passing it in the flags parameter of the FileObserver constructor.

How can I scan through a raw text file?

I have text files in the raw folder that I would like to use in my app. I am using ListFragments and depending on what item is selected I want to load that files text in a new layout.
I can properly load the text fine but I would like to be able to scan the file previously so that I can have the first line of the file be the title that is displayed in the ListView. I have a method that determines the number of files in the raw folder and adds the names to the ArrayList.
Here is my method:
private void listRaw() {
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
myArrayList.add(fields[count].getName());
}
}
The problem i am having is setting up a scanner. I do not know how to tell the scanner to scan this specific file. I tried making a new File object and used the Scanner(File) but I do not think I am declaring the file name properly nor if this is even the best way to get this done. Normally if you know the file name you can just simply do:
Scanner fileReader = new Scanner("filename");
but in this loop I never actually have the file name set.
I have text files in the raw folder that I would like to use in my app
I am assuming that "the raw folder" means the res/raw/ resource directory in your project.
I do not know how to tell the scanner to scan this specific file
That is because there is no file. The file exists on your development machine. The representation of the raw resource at runtime is just that: a raw resource. Under the covers, it's effectively an entry in a ZIP file.
You need to get the R.raw value for a raw resource, then use getResources().openRawResource() to get an InputStream that you can pass to a Scanner.
Unless you have different editions of these raw resources for different configurations (e.g., language), you might find it easier to work with the assets/ directory and AssetManager for packaging text files with your app.

reading image path from a database

I'm stuck in something that I'm sure rather easy
I have a field in the database that I stored the image name in it, ex 'images/ex1.png'
how can I read this into an imageView?? I tried some code in there but can't get it to work
a good example is highly
You cannot set the image file directly as the resource for your ImageView (that only takes the resource ID).
Instead you will have to use a BitmapFactory to decode it and then use setImageBitmap
EDIT :
Your problem is that there is no way for you to map the name of the image file with the resource ID in the code. The easier way is probably for you to put together a map table.
First put your image files in the drawable folder (res/drawable/ex1.png...) and then, do something like this in your code :
Map map = new HashMap();
// Add key/value pairs to the map
map.put("images/ex1.png", new Integer(R.drawable.ex1));
map.put("images/ex2.png", new Integer(R.drawable.ex2));
// do that for all the images....
String image_name = // query to your database
ImageView the_image_to_change = // findViewById(...)
if (map.containsKey(image_name)) {
the_image_to_change.setImageResource((Integer)(map.get(image_name)).intValue()); // not sure so much is necessary...
}
If you're storing this in your /res/ folder, you need to use a ContentProvider in order to get access to this resource. You shouldn't be accessing resources directly unless you're downloading them externally from the app.
Hope this helps.
myImageView.setImageBitmap(BitmapFactory.decodeFile(myFilePathString));
Your issue is that your file path does not include the root of the device's file system. You have to add this to the beginning of your file path string. This depends on how you save the images. So...
To acquire the appropriate file path to a file on the sd card:
File sdCard = Environment.getExternalStorageDirectory();
String filePath = sdCard.getAbsolutePath() + imageFilePath;
If you're saving your images to internal memory, your app is automatically given a folder to save files to and they will be located there. To find the root to that path use getFilesDir(). For more information on data storage check out http://developer.android.com/guide/topics/data/data-storage.html.
Use the res/drawable folder. Here's how to load an image from drawable to a Bitmap.
Bitmap myImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_image);
More on resources: http://developer.android.com/guide/topics/resources/providing-resources.html

Categories

Resources