How to permit the user to navigate into the File System? - java

I'm making a project that opens a file and reads/writes data.
I'm doing this using the code below:
File file = new File(Environment.getExternalStorageDirectory().getPath()
+ "/Project/Application/" + fileName);
It's working. The problem is I only can get this file if its path is the specific 'Project/Application' directory.
How can I allow the user to navigate into the device file system and find the file himself?

Android does not have a file chooser by itself. The only file list that Android can show natively is a ringtone list.
Here you have some solutions:
Load the file list into an array and show a dialog showing the file list, as you can read here: Choose File Dialog. Using this method you can detect when the user clicks an item that is a folder and then show a new dialog with the files inside that folder, so the user can navigate easily.
Here is a good answer to your question that has a built-in file chooser just in case that the user does not have one installed: Android file chooser
And also here you have a good library: https://code.google.com/p/android-filechooser/

Related

Show pdf folder with its content to user

I have an app that save some pdf files in sdcard/android/data/com.myapp/files/pdf. I'd like to show its content to user, so he/she can open the pdfs, read it using another app, share...
This is my code to create the folder:
File folder = new File(Objects.requireNonNull(MainActivity.this
.getExternalFilesDir(null)).getAbsolutePath() + File.separator + "pdf");
SO, I'd like to open this folder to the user, because it is quite a hard task to open it all manually. Any ideas how can I do it in any android api version?

i am not able to view my downloaded file in downloads folder using selenium java

[Screenshot displaying the downloaded file in a window[][1]1i am not able to access my downloaded file which i opened in a new small window popup using selenium java and also i am not able to find it in downloads folder.
When i click on a download button-->the file gets downloaded with an icon of word in a new small popup browser window-->but it does not move into the downloads folder. Testcase passes but i am not able to validate.In that browser window i am not able to find any elements....only .... tags are displayed.. While doing it in manual way i am able to view it in downloads folder.
Kindly advise me what should be done.
you are coding in java language,
so, use method exists() which will return boolean value.
You must know your Downloaded file name along with exact download path,
String FileName = "YOUR_FILE_NAME";
File tmpDir = new File("C:\Users\a.k\Downloads\"+FileName);
if(tmpDir.exists())
System.out.println("File Downloaded");
System.out.println("File Not Downloaded");

opening text file in my textapp

I am new in java swing .For practice, I've created text app which simply allow the user to open,read and write text files and also have some editing functions.
I've packaged my .class and other required files into jar file and created .exe file from this jar file.
When user opens text file(.txt,.rtf e.t.c) from the app(by clicking Open file toolbar on my app ) it works fine and display the content of file in JTextpane.
But when user opens text file outside my app (by clicking open with and set my app to open particular text file) to display the content of file on my app,my app just getting opened but not displaying the content of the file in JTextPane .
Can anyone suggest me the way How can this be done?
When you Open With... a file, that file's location is passed to the program as the first argument. So in your public static void main(String... args) procedure, you could add handling of a first argument that is a file location and open that file as you would otherwise from the GUI.

How to save files to a folder and query it within app, without the risk of losing them if they're renamed?

My app saves voice recording on external storage. The user inputs a filename after the recording, and I add that file name to arraylist and save that arraylist using the SharedPreferences.Now, if a user goes to the directory and renames the file, the name of the file will not change in the SharedPreferences. This is a problem because when the list of files is shown in my app, I open it by using the name saved in SharedPreferences. So if a file is renamed, that name will not be in the SharedPreferences, and will crash my app. Is there anyway I can avoid this, for example - can I save a file and retrieve it and open it irrespective of what it's name is? How would I do that?
I'm surprised by the lack of similar questions, which makes me feel I'm missing an obvious easier way of saving and retrieving files.
This gives you a list of all files within a specified directory:
File directory = new File("/path/to/directory/");
File[] files = directory.listFiles();
for( File file : files ) {
doStuffWithFile( file );
}
No need to know the file names, just the directory.

I want to supply some pdf documents with my application

After certain research and lookup over the internet I found that I cannot place these files in the assets folders as those files will be private to my application, I want the native pdf application to be invoked on a button press which redirects the user to the particular document. What I am confused about is how do I bundle the files with my application so that when the user downloads or installs my application these files get stored in her SD-Card.
copy your assests files to the sdcard and when clicking on the button open the files in the sdcard as like this
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/filepath/"+fileName);//give the file path here
if(file.exists())
{
// do action here
}

Categories

Resources