I am developing an application which creates menu with each menu item having its own icon. I have too many PNG files for each menu item. In future i will be updating menu items from WebService which will add to these png files.
What is the most efficient way to store this data(png files). I don't want to keep it in drawable folder as I can't update contents of drawable folder after i have shipped my app. If i am storing it on External Memory, How do i achieve that? Where exactly should i copy these png files and use them as resources in my code.
Thanks
Store them as BLOBs in sqLite.
You can write to private space assigned to your app using this http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int) method and read back with it's sibling.
InputStream is = your input stream
OutputStream os = mContext.openFileOutput("image.png",Context.MODE_PRIVATE);
Utils.copyStream(is,os); //Your own steam copy here
is.close;
os.close;
Related
I want to load Bitmap from Project resources using BitmapFactory.decodeFile(String pathName).But I don't know how pathName should be composed. Here on StackOverflow I found some answers but they are about loading images from SD card. So How can I load Images from Project Resources by their paths(for example from drawable folder). I don't want to use BitmapFactory.decodeResources() because I have a lot of images. And I want to have access to all. With String pathname it will be easier.
But I don't know how pathName should be composed.
There is no pathName, as resources are not files on the filesystem of the device. They are entries in an APK file. You have to use decodeResource().
I don't want to use BitmapFactory.decodeResources() because I have a lot of images.
Having a lot of images does not change matters, nor should it impede your use of decodeResource().
Why don't you use BitmapFactory.decodeResource(R.drawable.test);? It decodes only the resource you want. See here.
I would like to create a button that when clicked will go to a class that displays all media files from an SD card using a ListView.
After selecting from the list it will then return the filename selected to the main class. IF the returned file is an image file, it will be displayed in an ImageView and if the returned file is an audio file, it'll display and on click play?
You may find it interesting to use a library to consult the files in a directory and display it. This can save you lots of work if the requirements of your application permits. There are a lot of libraries for this pourpose, for example:
https://github.com/bartwell/ExFilePicker
Hope it helps you!!
This is not a coding problem question, I just need general advice.
I am making an application that populates a listfragment with multiple text files. What I want is for the user to select the intended article they wish to read and said article will be provided for them. I have already made a master/detail esk application but it is only reading in a static array for the articles (which will later be changed to an expanding list and more dynamic method for reading in the files)
My question is this: do I make a database for the ~30-40 text files and create an onlistclick--database adapter function or should I use Assetmanager (or something else).
I am new to android programming and this is my first proper application so any suggestions that would be suitable for a novice would be much obliged.
If those text files are going to be modified very often then yes, it is a good idea to create a local database to keep them into.
On the other hand if you only need those 30-40 fixed text files and you are planning to add more just through app updates then you can simplify your life by using the assets folder. Cycle through all the files in there and use them to create the list.
Code snippet for Assets usage:
AssetManager assets = context.getAssets(); //use 'this' if you are inside an activity or 'getActivity()' if you are inside a fragment
String files[] = assets.list("folder_inside_assets"); //use assets.listFiles("folder"); if you want an array of File objects
I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.
How can we modify a text file from android, such as
append at the end of the file.
clear data at any intermediate position and also blank lines in it.
Is there any option to delete a particular line from the text file, I read so many tutorials regarding this all have mentioned to create a temporary file and asked to copy the contents from the original file except that particular line, and to replace the original file with this temporary file.
Is there any alternative to do this with in that original file itself without creating a temporary file?
Thanks in Advance.
You can't do the second option programmatically, that's virtually impossible. The first option however is possible. Just use an InputStream Object to load your file, then you can get the text from the InputStream as a String, I believe, and then you can append the data at the end. Now that I think of it, it might be possible, but with a heck of a lot of work, to edit the text at the middle... But that's up to you to figure out...
Pretty much everything that is possible with java's file api's can be done in android
You can use a FileOutputStream, passing true for append and then using the normal output stream operations.
For changing files in a random-access manner, try RandomAccessFile. This allows you to change the current byte you are pointing to. However, if you delete data in this manner it won't automatically cause the file to contract to fill the empty space; you would need to do this manually and then change the length of the file via setLength().