Android Assets No Value Read? - android

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.

Related

How to get path to file in android apk

My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.

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.

android assetManager

I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.

No space left on device but SD card not full

I have android app which downloads files from google docs to SD card on device. (reason: have PDF-s which I cannot open within app).
It writes successfully to SD card until I get to ~8000th file of 10000 total.
Code for writing to SD:
InputStream is = response.getContent();
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+preferences.getString("username", "manas11")+"/" + tmp_entry.resourceID.replace(":","~_~")+"~_~"+tmp_entry.title));
byte[] buf = new byte[1024];
int len;
while((len=is.read(buf))>0)
fos.write(buf,0,len);
Exception thrown by line 2:
java.io.FileNotFoundException: /mnt/sdcard/matulic.realestate.hr/file~_~0B3n2EnTf5ATnYTJkNTNmM2QtZDJjYy00YjNhLWJlZjQtYTE4MTU5MjI2N2E5~_~Kuæa Marina Sevid 399.00 m2 - LJETNA AKCIJA IZVRSN - k359.txt (No space left on device)
SD card is microSD™ memory card (SD 2.0 compatible), 8gb, FAT32 formatted. Im using Desire HD.
As you can see filenames are long.
I save files to directory in /mnt/sdcard/ which state after exception is (by Astro):
size: 519,668,573 bytes
number of files: 8037
device free size: 4.91 G
Is there any limit for writing to SD I dont know about?
Thank you
I believe this stackoverflow question will answer your question.
Is there a limit for the number of files in a directory on an SD card?
I also checked out some of the information myself and found it basically comes down to a limit in the number of files and the length of the file name. From what I read in the Microsoft document referenced in the link above http://msdn.microsoft.com/en-us/windows/hardware/gg463080.aspx you have a problem with the length of your file names.
First is the length of your file names. Longer file names take up more space. There is a limit of 65,536 32 byte entries in the directory. This is because the maximum size of a directory file container is 2,097,152 bytes (65536*32). However, if your file names are longer than 8.3 they will take up more than just one entry. Also long file names take up more space than simply the sum of their characters. There is overhead to the filesystem that creates things like checksums and a short 8.3 file name to go along with it.
Best advice would be to shorten the file names dramatically and that should solve your problem.
Hope this helps.
It might be a limitation of FAT32.
A FAT32 directory can have 65,536 directory entries. Each file and
subdirectory takes from two to thirteen entries, depending on the
length of its name, so those entries can disappear long before you
think you've used them all up. Your total of 22,657 files could very
easily use 65,000 entries.
From here.
Have you tried reducing the file name when storing it locally? Does it really have to be that long?

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();

Categories

Resources