I am working on an Android app, where media(audio/video/images) could be stored either internally/externally. I would be facing the following scenarios
Case I
Setting dynamically images from the random value broadcasted by the app.
Right now, I am managing it as
if(rowData.strName.equals("football")){
imageView.setImageResource(R.drawable.football);
}else if(rowData.strName.equals("chess")){
imageView.setImageResource(R.drawable.chess);
Problem As of now, I am having few records so managing else if loop in not big headache, but later it could turn out to be one.
Case II
Downloading a media from internet, saving it in external storage and loading it on an imageview as an when required
Problem Incase, the image has already been downloaded(app keeps track of downloaded image), the user ejects the card, then I plan to use
try{
}catch(FileNotFoundException e){
imageView.setImageResource(R.drawable.no_media);//media from the app
}
Case III
I will be having a listView of a category.Each Category contains certain sub-category names,their images(inbuilt & to be downloaded externally) & their description.Each sub-category has sub records with each record having its own one or image(inbuilt & to be downloaded externally),description and media files(both audio and video).
I am confused on what Collection class shall I use for this case and how to begin with? For data that is to be retrieved from the server(online), I plan to use XMLParsing. Please suggest me the best way to achieve the problem.
Wow! This is really three questions. If only I could get three times the rep :)
Case I
Use a Map to store and retrieve stuff. Saves you writing lots of if/else statements.
Map<String,Integer> drawableMap = new HashMap<String,Integer>();
drawableMap.put("football", R.drawable.football);
// Then later to retrieve...
imageView.setImageResource(drawableMap.get("football"));
Case II
OK, this seems fine. Though for most of my image loading I use existing libraries like Universal Image Loader or Volley. These may do what you need, I'm not sure.
Case III
I would take an OO approach and model the data appropriately. You don't have to choose "a" Collection class to use. Create a Category class. Create a SubCategory class. Have the Category class "have" SubCategories, etc. Take it from there.
XML parsing is fine, I'm not sure what you're expecting to be suggested. You may also like to consider JSON, a popular data format.
Related
Hello i am using MIT App Inventor 2 to make an app that runs on an old (spare) android phone to display the folder.jpg of currently playing movie (later on I also want to do that with music).
I can't filter the path to use, for example this is the result of the json:
{"id":"VideoGetItem","jsonrpc":"2.0","result":{"item":{"id":1,"label":"2012","thumbnail":"image://F%3a%5cMovies%5c2012%5cfolder.jpg/","type":"movie"}}}
I want to keep: image://F%3a%5cMovies%5c2012%5cfolder.jpg which is the dvd cover (and maybe the label: 2012 which is the title of the movie).
I tried various options like making a list but then I still get text with lot of params between '()'
Can someone help me with this?
Thanks
Use the JSON decode block together with the lookup in pairs block. As you can see, it helps to use Do it...
I have something that is doing it for me for starters.
I see a flaw but that's in Kodi(.tv) itself (I am not a fan of the library system in kodi, so I put a folder.jpg in every drawer of a movie, but sometimes kodi doesn't display it and presents me with a video preview of the movie, I have to sort that out with kodi developers)
For now I am glad I have it working,
solution picture
I am developing an app for Google glass using using a Immersion Pattern.
I am using start activity to switch between different tiles using below code.
Intent showImageDetails = new Intent(MainActivity.this, CapturedImageActivity.class);
showImageDetails.putExtra("IMAGE_DATA", data);
startActivity(showImageDetails);
data variable holds byte array for captured image.
Few time device is not able to start the activity and it exits the application and goes back to OK Glass tile.
Have anyone observed this issue?
I have used charades as an example which comes with API example.
Based on your comment, my wild guess is your image is too big to be sent with intent. Have you noticed the JAVA BINDER FAILURE error in the log or TransactionTooLargeException:
The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.
Also see Passing data of a non-primitive type between activities in android:
What you do not want to do is pass big stuff via extras. For example, if you are creating an application that grabs pictures off the camera, you do not want to pass those in extras -- use a static data member (icky as that sounds). Intents are designed to work cross-process, which means there is some amount of data copying that goes on, which you want to avoid when it is not necessary for big stuff.
I think if your image is large, it is better to pass the URI or ResourceID of the image but not the image itself. Hope this helps.
I am creating a multiple choice quiz game where there is an option to continue the game from where the user has left. Example, if user stopped the application at Question Number 4, then by pressing the continue option will resume the quiz game from Question Number 4 itself. I am not able to understand how should I move ahead by coding way? What is the right way to continue a multiple choice quiz game? Any help would be appreciated.
As an example, you could use JDOM. Just include the .jar file in your projects build path and then you'll have access to the JDOM library methods.
An example of using the JDOM libraries:
// Create a new XML document in memory
Document doc = new Document();
Element root = new Element("child");
doc.setRootElement(root); // set ^ above element as the root element
root.addContent(new Element( "childname" )); // add a data element to the root
root.getChild("childname").setText("SOME INFO"); // give some data to the element <childname>
// Save the XML file
FileWriter writer = new FileWriter("FILENAME.xml");
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat()); // sets correct tabbing/format in the file
outputter.output(doc, writer);
This would make something like:
<child>
<childname>SOME INFO</childname>
</child>
You can then access the information stored in the XML file by using some JDOM methods like:
root.getChild("SOME CHILD").getText();
Of course JDOM isn't the only option here. It's whatever seems easiest for you, not me.
Where are you getting your questions from? Try to build a sqlite db around your game. Get your questions from there. Then when the player goes out save state of the quiz in a db table, and when he comes back resume the game. Also you can store his previous performances, current performance in seperate tables. Seems like a good way to go about it.
Its a simple solution actually. A sql read will always result in the same sequence of read when you make a select call. So if you have 100 rows, and you select 10 rows of them and choose the first one, it will always be the same. Practically tested and it works. Now, you can create a "played" flag in your table and flag your quiz played. This way you will always get the same question in the same sequence as you reset.
If you want to randomize, I suggest you code that. There is no automatic way to do it.
Now for question, you should use shared preferences to save current question being played. This way you can just read that one row when you restart your application.
My music app is referencing persistently stored data. All are currently stored as text files:
Favorites - single text file array. App starts, reads the text file, stores in memory. Array is checked when ListView is expanded. If array changes text file is rewritten.
Last Played - single text file array list. Updated every 5 seconds. Retains the history of the played songs to allow the user to return to any album and resume position.
Playlists - currently individual text files, one for each list. List of playlists generated from file names when required. Each playlist text file has array list inside it. Read Write when required.
Most Played - single text file array list. Updated once per song played.
I am wondering whether this data would warrant the need to change to a database, or whether I have taken the right approach. I don't foresee the need for adding additional data so this should be the most I would need.
Advice please!
You can store the text files in SharedPreference and it should work well.
Database if preferable if a lot of data needs to be stored. Using a database is more optimal than parsing a string.
The correct approach is to create a class for each of the things you want to represent, e.g. favourites. Each class has a save() and reload() method. The point is that you can change the underlying storage mechanism in the save() and reload() methods without having to change the rest of your code. Imagine in the future, you want to enable saving to Dropbox. You would simply change these methods and your app would just work (OK, you'd need to add stuff to get Dropbox account details but you get the idea).
You could go further and define Interfaces for loading and saving. That interface can use a single class responsible for nothing except saving and loading. As long as any consumer and provider classes adhere to the interface, you can mix and match, and even implement storage approaches yet to be invented, without recoding your app. You only have one class to work with if, and whenever, you change your storage approach.
class StorageManager(){
enum DataType {Favourites, MostPlayed, PlayList };
...
public save(DataType dataType){
if (dataType == DataType.Favourites){
saveFavouritesToDB();
...
...
}
This approach gives you maximum flexibility, maximum future proofing and better maintainability.
I think it would be better to go with Database.
The database should provide you with some optimizations, performance improvements and basically you don't have to reinvent the wheel (doing read / write operations on the disk, use some buffers before rewriting, and the like).
Plus, I think you will love the way all code will be organized and split into its own layers once you start using this way.
I'm a flash developer with no previous Java experience, just starting to learn android development. I'm trying to make a simple kid's flash cards app, consisting of a load of images of animals, and a load of sounds that they make.
Currently I have the images in a gallery view, storing them in an array. I also have an array of the sounds. So each image and the corresponding sound are in the same position in the array, so it's easy to play the right sound for the right image.
Now I want to shuffle the cards so that they appear in a different order each time the app is started up. I managed to shuffle the arrays into a random order, but kept the images and sounds in the same positions in each array but I can feel this getting messy and I'm sure this isn't the best way to go about this problem.
If this were a flash movie, I'd use objects to link the images and sounds and stick the objects in an array. Can anyone help me with some code which would achieve the same thing for android? Bear in mind I'm a complete beginner with Java and have gotten this far with tutorials and basic concepts being the same as AS3.
I'd use objects to link the images and sounds and stick the objects in an array.
Me too. Just create a class to wrap animals and sounds:
class SomeNiceName{
private final Bitmap animal;
// I'm guessing the sound is in the resources
// folder, thus you only need an integer to reference it
private final int sound;
public Animal(Bitmap animal, int sound){
this.animal = animal;
this.sound = sound;
}
public Bitmap getAnimal(){
return animal;
}// missing getter for sound
}
In this case I'm using an immutable object which is convenient in this case. Then you can create an array of those animals, o better yet a list:
// array
SomeNiceName[] array = new SomeNiceName[blah];
array[0] = new SomeNiceName(someBitmap, theSound);
// or with lists:
List<SomeNiceName> list = new ArrayList<SomeNiceName>();
list.add(new SomeNiceName(someBitmap, theSound));
The only thing you would have to "disorder" in this case is one array.
As Christian said you can of course use class in your Android application.
But, since mobile devices haven't huge processing capabilities like desktops or laptops -yet-, I advice you to read the articles below before running your OOP habits ;)
Object Creation
Getters/Setters? Not here!
For items above and more...