Couldn't update a file in android app assets folder - android

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.

Related

Moving a file from a PC to an Android-readable location

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")

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

Referencing files in Android

Why can't I refer to a PDF file in my Android device?
I have a folder named "templates" in the root folder, and I have a PDF file in that. I've tried to refer to the file via the following:
File file = new File("/templates/myPDFFile.pdf");
but when I try to print file.exists(), it always returns me false.
Just as further information, my file hierarchy is:
root
-src
-com.example.text
-MyActivity.java
-templates
-MyPDFFile.pdf
and my code is stored in MyActivity.java.
How do I reference accurately to the PDF file?
You must put it in assets and get it from there like :
How to open a pdf stored either in res/raw or assets folder?

Is there a file url shortcut for my app's home directory?

You can references asset files with a file url by using "android_asset", as in file:///android_asset/whatever. Is there something equivalent for the app's home directory? I'll be using the references in a static html file, so I don't want to call getFilesDir(). I'm pretty sure the directory will always be /data/data/[package]/files, but in case that is different on a weird device, I'm wondering if there's a file url shortcut.
You can put your file into res/raw folder. For example your file is page1.html, to use it in your activity:
InputStream is = getResources().openRawResource(R.raw.page1);
More references: data storage.

File not found issue in Android

I need to access myfile.txt file using FileReader in Android , please suggest me where to add the text file in Eclipse. I tried it adding it in Resource and Asset but I am getting File not found issue.
FileReader fr = new FileReader("myfile.txt");
Even
File ff = new File("myfile.txt");
File Supports only the below listed parameters
FileReader Supports only the below listed parameters
Note: I want solution for this issue , only with FileReader or File
The directory would be /res/raw/ this is where you put all your extra resources.
you can refer to it using getResources().openRawResource(resourceName)
and check here Android how to get access to raw resources that i put in res folder?
EDIT:
how can i edit the text files in assets folder in android
in short
the easiest way would be to copy the file to external directory then do your stuff there
link is here
Android: How to create a directory on the SD Card and copy files from /res/raw to it?
One thing to mention - prior to 2.3 the file size in the assets cannot exceed 1MB.
hope it helps abit
That's how I obtain my file from the SD card, perhaps this can be some use to you.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File options = new File(getAppDirectory(), "portal.xml");
}
The getAppDirectory method used in the bit of code looks like this :
private String getAppDirectory() {
return new String(Environment.getExternalStorageDirectory().toString()
+ "/foldername/foldername/");
}
After this bit of code I also make sure the file exists and what not before I attempt to read from it.

Categories

Resources