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.
Related
I am working on an app which moves a file from sdcard to internal memory storage .What I need is :
1.To detect the size of the file to be moved.
2.Detect the size of free memory in internal storage.
3.Compare both?
This would give some alert whenever the free memory is less than the size of the file.
How can I do the first two points.If anyone worked on similar type of problem?
long file_size = file.length();
File data_path = Environment.getDataDirectory();
StatFs stat = new StatFs(data_path.getPath());
long free_internal_memory = stat.getBlockSize() * stat.getAvailableBlocks();
I've done something similar, but then for multiple (music) files in a directory, and across any mount point available to the system. I'm not 100% sure the explanation below will be applicable for the internal storage, but it should at least give you some useful pointers.
You can easily get the size of a file by calling .length() on it:
long fileSize = someFile.length();
Note that in case someFile is actually a directory, the result is not defined. Hence, to get a directory's size, iterate over all its children (which on its turn may again contain directories of course).
For retrieving information about the file system's space, I used the statFs class, which is nothing more but a Java wrapper for statfs(). Calculating the available space is a simple multiplication:
// 'fromFreeBytes' is the source file/directory size
StatFs toDirStats = new StatFs(mToDir.getAbsolutePath());
long toFreeBytes = ((long)toDirStats.getAvailableBlocks() * (long)toDirStats.getBlockSize());
if (toFreeBytes < fromFreeBytes) {
//insufficient available space
}
Finally, it's important to remember that if you're going to move files between different mount points, you cannot use File's renameTo(...) method. In stead you'll have to use an InputStream and OutputStream to copy the data across.
Good luck!
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.
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.
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();
AssetManager assets = myContext.getAssets();
String[] files = assets.list("MyFolder");
InputStream myInput = assets.open("MyFolder/" + files[0]);
int i = myInput.read();
in this case 'i' is -1 meaning nothing read.
Why would nothing be there if the file is there, the variable 'files' has the file as well.
Do I need to do anything to the file I put into the Assets folder in get it to be readable?
NOTE: When I use a small text file it works. When I use a 10 meg file, it does not. (The 10 meg is a Sqlite database I need to install)
Rename the file to XXXXXX.png so that it is not compressed, then it can be copied over.
You cannot put a 10MB file inside an APK. You will need to slice that into 10 1MB files. Better yet, distribute the database in some other way, such as downloading it to the SD card on the first run of the application. Many users will be unable to install your APK if it is that large.