I am creating a simple Android app. To run properly, it needs the contents of a file (example.xml) that is currently on my PC.
On creation, the app would look for the file, and then do some stuff with it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
File file = new File("I wish I knew what to put here");
doStuffWithFile(file);
}
Unfortunately, I don't know where to put the file.
I need a location in internal storage to which both my PC and my Android app have access.
Is there such a location? If so, what file path String would I use to access it?
You can get the Download folder by using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Then you can add a filename and use this e.g. with DownloadManager
You could include the file in the assets folder of your project and then retrieve it with this.getContext().getAssets().open("example.xml")
Related
i have a interactive map folder. i try to create a webview app with local files.
inside the folder are all the files needed to load the map.
gta5online.com/map-interactive
also included is a html file that i tried to open in an app. i just pasted the folder inside the main application folder for android studio.
i tried to put the path in the code as you can see for the string. when i run emulator it says it cannot find the file. what do i need to put there. can someone give me the exact one please? i'm a noob.
the path on my pc is: C:\Users\me\AndroidStudioProjects\MyApplication\interactive\map.html
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url ="file:/interactive/map.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings() .setJavaScriptEnabled(true);
view.loadUrl(url);
The best way to reference html content in this case is to put it in an ./assets folder and use the URI file:///android_asset/ to refer to it.
See for example the reply here: How to get URI from an asset File?
To be fair, I haven't tried that since Android Studio became a thing and gradle changed the default project layout -- hopefully that doesn't further complicate matters for you.
I am developing an android application. I need to create a folder in the internal memory, but when I try to create the folder I get the error below. I am running in an emulator.
mkdir failed for /mnt/New Folder , read only file system
I have tried many paths, but still the error persists. The only folder that I am able to create is called "cache", but I cannot browse it by my file chooser activity.
Any idea where is the suitable place to create folders without any permissions?
You can achieve it by this from a Context object (like Activity).
File files_folder = getFilesDir();
File files_child = new File(files_folder, "files_child");
files_child.mkdirs();
File created_folder = getDir("custom", MODE_PRIVATE);
File f1_child = new File(created_folder, "custom_child");
f1_child.mkdirs();
The function
getFilesDir()
will get the folder data/data/yourpackagename/files in internal memory. And the function
getDir("custom", MODE_PRIVATE)
will create a folder name app_custom in your app internal folder.
Answered by Minhtdh
I guess what you call internal memory is atualy the external memory (which can be open by
file chooser activity, the real internal memory only can be open if you have rooted)
If that true, you should chek those belows:
- first, you will need the write storeage permission in Manisfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- then you should use `
String path =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourfoldername"
`
than
mnt/yourfoldername
at last you should use mkdirs to create folder than mkdir
I want to update a file in assets folder when the user click a button.
my listener is a classic
OnClickListener myUpdateListener = new OnClickListener() {
public void onClick(View v) {
updateAllParams();
}
};
The new file is to an url in this form
http://sample.test/android/params.dtf
the file in assets folder is
params.dtf
But I don't know how to do replace this file with the new version on the site.
I want to update a file in the assets folder
No, you cannot.
The assets folder is read-only, you cannot write or update any file present there.
You cannot update the assets folder after the application has been packaged and installed. You can, however, store to the device's memory and read in your file from there. More on this subject can be found at http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
According to me Asset folder is read only.Store your file in sdcard.
I'm building an android app which finds the nearby wifi hotspots. I have an xml file which includes GeoLocations of the hotspots, the xml file is located in the res/xml folder of the application. I would like to download an updated xml file when application starts. I have two questions:
Shoud I download the xml file into Internal Storage or External Storage and why? If I should download the xml file into Internal Storage, how can I do that?
I would like the user to be able to use the initially embedded xml file when the application first starts and switch to the newly download xml file when the download finishes. How can I achieve that seamlessly?
Thanks
Take a look at Google's page regarding data storage on Android.
http://developer.android.com/guide/topics/data/data-storage.html
You can download the XML file in a pretty straightforward matter. Another question like yours was asked here:
Download a file with Android, and showing the progress in a ProgressDialog
In that example, the file gets saved to the SD card. Be sure to change the OutputStream pointing to the SD card, so that it points to the private storage, as the reference page describes:
FileOutputStream fos = openFileOutput(your_xml_filename, Context.MODE_PRIVATE);
To make the switch to the new XML as simple as possible, I'd encapsulate all XML-related functionality in one class and provide a simple method for switching to the new XML. For example,
class XMLUser {
private Location someLocation;
public XMLUser(String pathToXml) {
readxmlAndSetLocations(pathToXml);
}
}
Then you'd initialize a new XMLUser object whenever you'd need to change the XML file. Like so:
XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
XMLUser xmlStuff = new XMLUser(PATH_TO_PRIVATEDATA_FILE);
Another way would be to use the same object and just update the locations from the XML file:
class XMLUser {
private Location someLocation;
public void updateLocations(String pathToXml) {
//Read your xml here and update the locations accordingly.
}
}
e.g.
XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
xmlStuff.updateLocations(PATH_TO_UPDATED_XML);
Your questions allow for a wide variety of answers so I can't give a quick and exact one.
And yes, I do recommend saving the XML data to the internal storage. The XML file should be small enough in size to not pose problems, and logically, it's related to the internal functionality of the app. With which the user should not tamper. Plus, the internal storage is not removable, so in case the user changes the SD card, the updated data will still be there.
I would like to create a folder which is password protected. I am able to create a folder in the following way, but how can I create a password protected folder?
File nf = new File("/sdcard/NewFolder");
nf.mkdirs();
in android there is not possible but we can hide using "." at prefix
otherwise Store important folder or file in internal memory.
you can see you package directory following way
String dir = getPackageManager().getPackageInfo("com.example.kinkey", 0).applicationInfo.dataDir;
Note: it is visible on windows pc
You can't create such a folder on sdcard. Everything that's saved onto sdcard can be accessed by other applications. If you want to create a folder which is not accessible from outside of your application use Context.getDir() method with mode set to MODE_PRIVATE:
Context ctx = getContext();
File folder = ctx.getDir("NewFolder", Context.MODE_PRIVATE);