I am trying to access an image in another activity (say B) which is being captured in activity(say A). Now here, I have two options:
1. Save to sd card and then access in activity B through the filepath.
But, time taken to save is higher in some cases, probably because of higher
image size.
2. Send the bit array through intent.putExtra("imageArray" , data) and access it
in activity B through getIntent(). But on surfing net, I found sending bigger
bitmap of 1MB or more is a bad practise but didn't find anything with regards to
bitarray.
Can someone suggest me which option is better ? And is sending a bitmap of greater size as bitArray to another activity a bad practise ?
My requirement is : time lag between two activities A and B should be minimum. Also, image should be loaded in activity B in no time.
Thanks in advance.
Also you can use global variables in Application space (singleton).
Example:
public class YourApplication extends Application
{
private static YourApplication singleton;
Bitmap bitmap; // or any type, byte array
....
public static YourApplication getInstance()
{
return singleton;
}
}
...
in another class you can set and get this variable 'bitmap':
YourApplication.getInstance().bitmap = ....; // in Activity A
or
... = YourApplication.getInstance().bitmap; // in Activity B
or use inside another method
....( ..., YourApplication.getInstance().bitmap, ...);
If you load the image both in activity A and activity B by using a URL you can use ion - https://github.com/koush/ion. It helps you show pictures using a URL and it caches your image, so that loading it again happens instantly, just send the url from activity A to activity B.
If you use the phone camera to capture a image I would say that saving it is the better way to go, if your gonna want to send many pictures at once in the future the second option will be bad.
Related
I have two Activities, Activity_1 and Activity_2.
In Activity_1, I have an ImageView and a Button called "Go to Next".
In Activity_2, I have a Button called "CreatePDF".
So, I just want to know how to create a PDF with an image from Activity_1 when I press the CreatePDF button in Activity_2.
I would thankful if any one can solve this.
Thank You
From the few details you are giving here, I assume the Activity_2 does not know the image you want to export.
So first, you need to pass a reference to the image, or the image data itself, from Activity_1 to Activity_2.
See this post which may have what you're looking for.
The accepted answer is suggesting to use one of these three solutions:
Convert the image from Bitmap to byte array, then pass this byte array from one activity to another using an Intent object.
Save the image to the SD Card (or anywhere on the phone) and pass the image file path as a string from one activity to another, using an Intent object.
Pass the Bitmap image using an Intent object, without converting it.
I hope this helps.
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 using LRUCache to download images from server and showing on my activity views. I am not saving these images permanent storage(e.g. SD Card).
My Problem is that as my orientation changes, my LRUCache's object is destroyed and i am not able to get the images back after orientation change.
Before using the LRUCache i was storing the images in bundle, and hence, using onSaveInstanceState(bundle) it was easy to deal with that problem. But how to do like this with LRUCache object. Help!
Easiest way is to use a Fragment for that page. Then you can do setRetainInstance(true); to keep the OS from destroying the fragment on a config change.
Another way is to put the LruCache in an Application derived class. This is one way to keep persistent data between Activities too.
In onSaveInstanceState, try using LruCache.snapshot() to get a Map of your cache entries, ordered from least to most recently used. You could then write each key,value into the Bundle. Then when restoring from the Bundle, iterate each entry and put() them back into the LruCache.
Make a separate Class which contain a static variable holding your LRU class, your LRU will not be destroyed because it is a separate reference.
class MyLRU {
public static LruCache<String, Bitmap> mMemoryCache = new .....
}
To call the cache from your activity:
Bitmap myImage = MyLRU.mMemoryCache.get("imagekey");
My activity have several static bitmap arrays,but sometimes those static bitmap is gone when i open other activity, such as load photo using the intent below:
My Code
Intent intent = newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, ACTIVITY_SELECT_IMAGE);
When i stay too long in intent which i call to pick image, all my static bitmap array is gone.. i can't use final static because i change those bitmap on run time..
How do i prevent this? Thanks.
Android offers no guarantees on static references like that. The reason that it sometimes works and sometimes not is basically because sometimes the garbage collector(GC) has set your array to null and sometimes not. The GC will not set your reference to null if you activity is active, but it will if it has to when your activity is inactive, e.g. not visible.
You need to add a check for null in the onResume() method and make a new Bitmap array if necessary. As for the bitmaps themselves that are stored in the array, you need to store them in memory. Check the following article and read about saving cache files via the getCacheDir() method. That is basically what you need since the data does not need to be stored persistently across multiple sessions.
http://developer.android.com/guide/topics/data/data-storage.html
I have an app that is "skinned" at launch. The idea being that it looks to the server and loads several graphics for display. It then moves through a sequence of Activities. I don't want it to have to keep visiting the server for the art, however, every time it comes to Activity 1. I want to be able to store the images until such time as the user manually clicks a "refresh art" button.
I have every part of this worked out (the downloading, display, storing bits of other data in SharedPreferences), but I can't figure out where to save these images. They don't need to be available to any other application, I just want to have access to them from one running of the app to the next, until the user manually refreshes them.
TIA.
You could cache them on the sdcard.
you can save your images as static Bitmap objects in an extension of Application class, and referencing it from manifest:
manifest.xml
YourApp.java
public class YourApp extends Application{
public static BitMap[] myImages = new BitMap[someSize];
YourActivity.java
in somewhere..
YourApp.myImages[position]= myImage;
I think it can work.
But use this only if your images are small and few ones.
The right thing is to save them in the disk, I prefer to use the internal storage against the sd card, take a look of this http://developer.android.com/guide/topics/data/data-storage.html#filesInternal