Downloading files into a folder inside android application - android

Sorry if this question was asked many times,
But i want to store some files inside my application,,i tried to put them in Raw folder ,,ok it works i can load them when ever i want,,
But can i modify them at run time??
can i download new files and put them in the Raw folder?
I want to do that because if i put them in the SD card...the user can see them without the application, and i don't want that to happen..
Internal Memory is not an option too,,
i aslo used this path..but the files can be seen
directory = new File (Environment.getExternalStorageDirectory().toString() +"/data/"+getPackageName()+"/files");
directory.mkdirs();
if(DeleteFlag!=1 || !downloadTask.isCancelled()){
file1 = new File(directory, audioFileName);
output = new FileOutputStream(file1);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
}

But can i modify them at run time?? can i download new files and put them in the Raw folder?
No. Resources are read-only at runtime. Please store downloaded material either in internal storage (e.g., getFilesDir() or external storage (e.g., getExternalFilesDir()).
I want to do that because if i put them in the SD card...the user can see them without the application, and i don't want that to happen..
They can see everything without the application. They can see your raw resources without the application. If they root their device, they can see anything they want without the application. If you have data that you do not want the users to have access to, store it on the Internet and leave it there.
Of course, users tend to prefer apps that do not waste bandwidth and do not try to hide data from them.

Related

How to download file into data/data/com.****.*** folder and open it

I need to download some pdf files into data/data/com.**.* folder.
Those files are application specific and only application should read and display it that's the reason storing on data/data/com.**.* folder.
Please let me know how to download into that folder and open/read it in the application.
I know how to download it into SD card, but I do not have idea to downloading to application specific folder.
Please let me know some code examples to do this and also I need to know the capacity/size of the data/data/com.**.* folder.
As long as you want write your own applications Data folder, you can create a FileOutputStream like this FileOutputStream out = new FileOutputStream("/data/data/com.**.*/somefile"); than use that output stream to save file. Using the same way you can create a FileInputStream and read the file after.
You will get Permission Denied if you try to access another application's data folder.
I am not sure for capacity but you can calculate the size of the data folder using this
File dataFolder = new File("/data/data/com.**.*/");
long size = folderSize(dataFolder);
...
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
lengthlong += folderSize(file);
}
return length;
}
Hi here i am attaching the link of a tutorial explained.
http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
and there are many discussions going on internet that you should root your phone in order to access the data from data/data folder and I am also attaching some links about the discussion, I hope these are also some of the links that are related to your question
where do i find app data in android
How to access data/data folder in Android device?
and as well as some links that makes out the things without rooting your phone i mean
You can get access to /data/data/com*.* without rooting the device
http://denniskubes.com/2012/09/25/read-android-data-folder-without-rooting/
To Write file
FileOutputStream out = new FileOutputStream("/data/data/your_package_name/file_name.xyz");
To Read file
FileInputStream fIn = new FileInputStream(new File("/data/data/your_package_name/file_name.xyz"));
Now you have your input stream , you can convert it in your file according to the file type .
I am giving you example if your file is contain String data the we can do something like below ,
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String mDataRow = "";
String mBuffer = "";
while ((mDataRow = myReader.readLine()) != null) {
mBuffer += mDataRow + "\n";
}
Remember to add write file permission to AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android: avoid file download with MODE_WORLD_READABLE

Bonjour
I would like to create a simple market applications for android but I face a annoying issue.
The install flow is as below:
1- Download of apk file in the application context :
InputStream input = new BufferedInputStream(url.openStream()) ;
OutputStream output = openFileOutput("xx.apk", Activity.MODE_WORLD_READABLE);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1 && statusDownload ) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
2- When download is finished:
promptInstall = new Intent(Intent.ACTION_VIEW);
File fileApk = new File (getFilesDir() + "/xx.apk");
promptInstall.setDataAndType(Uri.fromFile(fileApk) , Consts.APK_FILE_INSTALL_TYPE);
startActivityForResult(promptInstall,654);
3- After installing (or if cancelled), apk file is deleted:
File fileApk = new File (a.getFilesDir() + "/xx.apk" );
fileApk.delete();
In order to install an apk file, it must be "World Readable", it means that everyone can , after downloading the file (and before installing), get the apk file.
Does anyone know how to set good permissions for avoid external downloads ?
Thank you for reading !
Does anyone know how to set good permissions for avoid external downloads ?
You already have the "good permissions". The installer runs in a separate process from yours, and therefore it must be able to read the file. Since you have no way of granting permissions only to the installer for this, you have to grant read permissions to all possible processes.
For most things, we would use a ContentProvider to get around this limitation. Alas, at least as of a year ago, the installer did not support content:// Uri values.

where to store xml updates in packaged android app?

I guess I'm a little confused as to how files are stored on an actual machine (or emulator even).
While programming, I can save my xml file in the assets folder manually, but how to write an app that will have to connect to the network and download the file,save it somewhere and then manipulate it ? where will it store said file ?
I want to create a new file, but I read on another post that the assets folder as such is not available once packaged; So where are they created and stored ? How can they be transferred. Its just, I'm new to this platform and the file system is a little confusing.
If you want to use XML that is updated, you should think of copying the file(s) from assets to device storage. You can take a look at How to copy files from 'assets' folder to sdcard? to know how this can be done.
Another alternative is to use the database where you can store the parsed data from the XML. So that you need not parse the file whenever you need to access the contents.
You have two options: call getFilesDir() from your activity to obtain a path to the internal data folder that can only be read/write from your app.
Or, you can write/read your xml file to external storage (SD Card). Use the method Environment.getExternalStorageDirectory() to get the root path of the external storage, then create your own folder as you see fit.
Note that if you write to external storage, every app in the phone will have access to it.
Even I faced this issue. Now I have a xml file which is has application properties.This is packaged in the assets folder.Once packaged we cannot edit a file in assets folder.
Now on app load I just copy this file to path returned by
context.getFilesDir().getAbsolutePath();
And the application edit it from the same place. You can see if the file is modified in the FileExplorer panel of DDMS view. The file is stored in the folder named same as your application package name for eg: com.abhi.maps
Alternatively you can also copy it to SD card.However it is risky because, sd card may bot be available all the time.
You can use the following code to copy file from assets folder:
private static void copyFile(String filename, Context context) {
AssetManager assetManager = context.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String newFileName = context.getFilesDir() + "/" + filename;
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Hope it helps! :)

Adding a big text file to assets folder

I'm developing an Android 2.2 application.
I want to add some big text files (4.5MB or more) to Android project.
First I don't know if I can add such kind of big files to assets folder. But, if I can, is it possible to compress them?
How can I compress files? and decompress?
Any other better way to add big text files to Android project?
Thanks.
Files over 1 MB placed in the assets folder won't be readable from your app (It'll throw an exception). This is because they get compressed during the build process, and thus the phone requires substantial resources to uncompress them when on the handset.
I believe you can place them in the raw folder, where they won't get compressed or use an extension that AAPT assumes is already compressed (see here)
However, It's not good having a 4.5 MB text file uncompressed sitting in the APK, It's wasted space that could be handled better. Try thinking about downloading the data on first use instead, or splitting the file into chunks as suggested before so that AAPT can compress it.
Another approach is you should copy your file into SD card during the first run using IOUtils. Here also be careful also because if you will copy each byte then more resources will be occupied.
It works for me, I needed to put 30MB large zip file into Assets folder because of Client's requirement.
You can, but sometimes it gives problems. You don't have to compress it, because the package itself is compressed (the .APK), in fact, anything that you store in the assets folder is uncompressed when you read it. With regards to the size of the file, you may want to cut it and put smaller parts of the file inside the assets folder.
I believe the assets directory (except for raw) is already compressed. Also the Android Market will soon/is allowing apks of 50MB in size. Try it first and then see if you have any problems.
You need to do fragmentation work for that 4.5 MB text file. Where you need to split the text file into five files with 1 MB maximum size. Then again you need to rejoin them like this:
OutputStream databaseOutputStream = new FileOutputStream(outFileName);
InputStream databaseInputStream;
byte[] buffer = new byte[1024];
int length;
databaseInputStream = myContext.getResources().openRawResource(
R.raw.outfileaaa);
while ((length = databaseInputStream.read(buffer)) > 0) {
databaseOutputStream.write(buffer);
}
databaseInputStream.close();
databaseInputStream = myContext.getResources().openRawResource(
R.raw.outfileaba);
while ((length = databaseInputStream.read(buffer)) > 0) {
databaseOutputStream.write(buffer);
}
databaseInputStream.close();
databaseOutputStream.flush();
databaseOutputStream.close();

Android: how to get storage used by an application

I have an application which has a directory created into SDCard where I save photos. I would like to know how much space is using that dir on SDCard in order to show that info to the user.
I'm not sure if its the best solution but you could do something like that:
int totalSize = 0;
File root = new File("path to one of your file").getParentFile();
File[] files = root.listFiles();
for (File file: files) {
totalSize = totalSize + file.length();
}
Then totalSize contains the sum of all files in the directory in bytes. depending on the structure of your directory (e.g. are there any subdirectories?) you have to adapt the code.
Edit:
After a little bit of researching I'm almost sure that there is no method in java which directly returns the size of a directory. See e.g. this link:
http://forums.sun.com/thread.jspa?threadID=640296
However in this link http://www.codemiles.com/java/get-directory-size-in-java-t1242.html there is a recursive version of my code mentioned above to calculate any subdirectories if availiable.
There is also a small library which can do what you want:
http://commons.apache.org/io/api-release/index.html
However then you have to import this library. I personally would prefer to write this short method by myself.

Categories

Resources