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
}
Related
In storage, I have one folder and this folder name is: my_app
In this folder I have one HTML file and i want open this HTML file into webView!
I write below codes, but after run application show me this error on webView: Web page not available and not open my HTML file!
My codes:
htmlReaderPage_reader.getSettings().setJavaScriptEnabled(true);
htmlReaderPage_reader.setInitialScale(1);
htmlReaderPage_reader.getSettings().setLoadWithOverviewMode(true);
htmlReaderPage_reader.getSettings().setUseWideViewPort(true);
htmlReaderPage_reader.getSettings().setBuiltInZoomControls(false);
htmlReaderPage_reader.getSettings().setPluginState(WebSettings.PluginState.ON);
htmlReaderPage_reader.getSettings().setDomStorageEnabled(true);
htmlReaderPage_reader.getSettings().setDatabaseEnabled(true);
htmlReaderPage_reader.setWebViewClient(new WebViewClient());
htmlReaderPage_reader.setVerticalScrollBarEnabled(true);
htmlReaderPage_reader.setHorizontalScrollBarEnabled(true);
For open from storage I write below codes:
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/my_app/index.html");
String uri = Uri.fromFile(new File(file.getAbsolutePath())).toString();
htmlReaderPage_reader.loadUrl(uri);
But when open this HTML file from assets folder, open this file and not have any error!
For open from assets I write below codes:
htmlReaderPage_reader.loadUrl("file:///android_asset/my_app/index.html");
htmlReaderPage_reader.loadUrl(uri);
Why open this file from assets but not open from storage!
I should open this from storage and a specific folder!
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 have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
I'm using Java move a file into a dropbox folder, which if a new file or photo that gets added, will trigger an IFTTT.com recipe that will post it to twitter. I am using Apache Commons IO for the movefile method on File Utils to create a File, and then move it.
But first, I have it find a path to a Content Path folder with a contentFileArray, which creates a File that gives us the image that I want to use. Then it takes the image, and creates a File destination that is in the local Dropbox folder. I set the file name and add the file extension.
Then I move it into the dropbox folder... which then triggers an IFTTT recipe that lets you post a Tweet when a file enters the Dropbox folder... or if a photo image enters the Dropbox folder. The problem is that a .png triggers as a file and as a photo, so I double post. It activates the File recipe and the Photo recipe.
IFTTT doesn't have any documentation for how Dropbox's Triggers work on the website, but maybe I'm missing something with FileUtils.moveFile(). Maybe it creates a file at that location, then has us change the file name. This could cause it to be read twice.
File src = new File(messageObject.getContentpath()+"\\"+contentFileArray[0]);
String ext = "."+FilenameUtils.getExtension(contentFileArray[0]);
File dest = new File(messageObject.getDropboxpath()+"\\"+messageObject.getMessage()+ext);
FileUtils.moveFile(src, dest);
successfulContentPosts++;
Does calling this line, cause this file to be made at this actual file path?
File dest = new File(messageObject.getDropboxpath()+"\\"+messageObject.getMessage()+ext);
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/