Android and big extra size - android

I m actually facing a problem with the intent extras size. In fact I need to get a nested object containing ~2000 objects.
So, when trying to transmit this intent, I got errors telling me that the size of the object is too big.
My question is : what is the best way to manage it, without using the ugly method, corresponding to store every object in a class using the static way.
What is the best way to transmit big informations between activities.
Thanks for advance

Related

Which is the better way to pass Image between 2 activities? Saving to file or Caching?

I know this is a very frequently asked questions and there are multiple answers. My question is "which is better?"
I've an activity where I capture an image using camera. I need to pass this image to another activity.
One way is to create Bitmap and pass it in putExtra since Bitmap is a Parcelable. This fails when the image size is too big.
I found 2 solutions. This answer uses MemoryCache to save and retrieve the image. Many answers (this, this and this) recommend to save the image to storage and then pass the path to new activity and read the image there.
Which is a better method in this case? (In terms of speed and memory)
It is safer in any case work with path or link than pass it as it is. It works not only with images but with most types of data. At the same time while working with path you can easy make if/else checks and handling some unexpected scenarios. Also pass the path is much faster.

Is there anyway to pass Image data through an intent?

I know how to do this converting it to a bitmap, but I want an Image (android.media) on the receiver side.
Thanks in advance.
If you want to pass only one bitmap at a time, I suggest creating a static variable in a class. and assign the bitmap object to it, and use it in the receiver class.
But if this is very big bitmap, it may cause OutOfMemory issue.
You probably don't want to do this. Even if you create a Serializable or Parcelable object that includes your data like this:
http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
which you may be able to do, by creatively encoding your image.
But an Intent has a limitation in size. See this post here:
Maximum length of Intent putExtra method? (Force close)
It says 1MB, but in my experience it might be as high as 4MB, depending on the platform (I may have that wrong, I don't have a specific reference to Android documentation to support it, but I see errors that appear to support it).
I'm sure you can move a lot of images within this restriction, but for any that fall outside of it, you will need a "work around" - which you should then probably make the "standard" and avoid the issue altogether.

startActivity(intent) not working in Google glass

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.

Is passing with serializable or parceable an efficient solution?

Im building an android app and in the startup activity i parse a pretty big json file (3.3 mb) into custom objects, or when there was no update i retrieve it from a serialized bytearray. Im dealing with one object with a list of about 500 objects with subobjects, lists etc.
Now i need this data, or some of it in my other activities. What is the best solution for this?
It seemed a lot of data processing to serialize and deserialize using intent.putExtra or using parceable everytime you start a new activity. Is this processing less than i think or is there a way to use your parsing class and don't destroy it so you can use something like
Myclass.get(nrIneed).Mysubclass.getsomestring
?
This is how i did it when using data for logging or something in my parsing activity.
You can use Application class to store this data and you can use it across all the Activity
public class BusinessClass extends Application
{
public ParsedData parsedData = new ParsedData();
}
Then call it in any activity using following code
BusinessClass appState = ((BusinessClass)getApplicationContext());
appState.parsedData.getData();
For more info
http://developer.android.com/reference/android/app/Application.html
You SHOULD NOT use Parcelable for objects which may consume memory more than about 1MB. Otherwise the parsing will fail (at least as per API level 8).
However, in your case, I would recommend you to save/organize the parsed data into SQLite and query it from other activities. This will help your app to eat less memory :)
You may also create a static reference to your object, but since its huge in size, I wouldn't recommend you, because then your app will become an appealing target for android VM to kill - when running under low memory circumstances.
I think you shouldn't use your data as a big Json file. At the first launch you should save your data in a database then only use this db when you need to Create/Retrieve/Update/Delete.
If you really want to use your JSON file, then you should make it static (in your application singleton for example).
Hope this will help you

Passing objects in android

I want to pass a client object to a diffrent activity on android
I know how to pass strings but have no idea about passing objects.
myIntent.putExtra("nick",nick);
where nick is a string
how do i pass an object say Client c?
"If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster)."
How to send an object from one Android Activity to another using Intents?
If the activity you are passing the object to is your own, serialize the object into a string and deserialize in the activity. http://java.sun.com/developer/technicalArticles/Programming/serialization/
Or as answered here: How to pass an object from one activity to another on Android
Think hard about whether you need to send the object or if you really just need a few data elements out of the object. Sending the primitives or Strings will likely be much simpler (= faster, potentially less buggy) if it's enough to meet your needs.
If you do need to pass an object then you could either implement the Parcelable interface or you could put the object into a static variable (of type List, Set, Map, etc.) in another class and reference it that way. There are drawbacks to these approaches and I would only recommend them if you can't get by passing the actual data values you need through the bundle.
Caveat: the standard line is use simple primitives/Strings as name:value pairs, prefer parcelable over serializable
The argument is that passing serialized objects is inefficient, so do not do it. For a small object I am not so sure that this is a real world concern. It has been argued for readability and clarity over efficiency unless you detect poor performance. So far with a small object I have not detected any performance problems. IMHO, using the fully qualified name of the class in the name:value pair and serializable objects makes the code readable, simple , less buggy, easier to maintain and is easy to implement during prototyping. If performance problems are detected, or you have time to refactor the code base, then the serializable code can be converted to Parcelable code once the object properties have stabilized.
OK. I am putting on my flame suit.

Categories

Resources