I want to know how long data can pass though intent. If i am passing parcelabel(as i am passing bitmap) more than 500x500 size it is give java transection binding fail.
If it's small (thumbnail) then it should be fine, if it is big you can end up with failed binder transaction errors. To avoid it you should pass a URI to location where it is saved. You can put it on sdcard if you have permission or in cache folder if not. If you are sending intent to some other process, then you should put bitmap on sdcard to make it readable by other processes. Last solution is to write Content Provider, this is usefull when you are sharing bitmap files, and want to for example attach bitmap with email.
Related
In Activity A, I download an image using Picasso and save into a Bitmap. I need that Bitmap in Activity B, how can I send it in the bundle without saving the image into the file system, and without sending the bitmap, as it's not very efficient?
EDIT: Is it possible to use the resource ID? If so, how?
If you strictly don't want to pass Bitmap to Bundle, you can extend Application class, and store/get bitmap to/from it. However, I'm asking you not to do this, and consider to send Bitmaps through Bundles, because storing such data in Application class is much worse then overhead from sending or storing in file system.
Bitmap implements Parcelable interface. So, you can pass it via bundle.
For sending,
Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
intent.putExtra("bitmap", bitmap);
startActivity(intent);
To retrieve it,
Bitmap bitMap = getIntent().getParcelableExtra("bitmap");
I have a fairly large list of image URL's that I'm using to load up a ViewPager using Picasso. I need to be able to provide sharing capabilities for these images via an intent (ultimately sharing via ShareActionProvider). From what I've read, Picasso isn't really built to handle this sort of thing out of the box, though it provides all the necessary tools to do so.
My plan before research was to create a simple LruCache which uses the url as the key and bitmap value. This caching would occur in onBitmapLoaded via Picasso's Target interface. Whenever I want to share an image, I'll check the cache for the bitmap. If it's not there, I'll fetch with Picasso. Now that I have a cached bitmap regardless, I'll write to a file (...this part doesn't seem right, though I have to write to a file to get a uri, right?) and add the file uri to the intent.
However I see that with the Picasso.Builder I can set (and retain a reference to) my own cache - https://stackoverflow.com/a/18552559/413254. This means I could do away with the custom Target and confusion with properly implementing hashCode and equals methods to ensure accurate recycling, retrieval, etc.
My question is, how does Picasso use this cache? What are the keys? Is there a way to get a bitmap Uri without writing it to disk?
If you want to use ShareActionProvider to share the image on the current page you don't have to keep your own cache of images. But to be able to share it to others, the image should be in the shared file system on the device.
It would be better if you use image loading libraries with custom disk cache support like Universal Image Loader
If you want to use Picasso (which is a good decision).
You either have to save a copy of the image on every page change which is nor a good option.
Or you can give a custom network handler to Picasso and set a custom cache implementation to it. I would suggest using OkHttp with custom caching which stores files in the format you desire. When you do that, you have to have a function that converts image URLs to file path on a device.
In every page change, if you have a Fragment inside your ViewPager, put the ShareActionProvider into your Fragments.
Get the reference of the ShareActionProvider inside onCreateOptionsMenu. And then set the Intent with the file path you get.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(Utils.getFilePath(imageUrl));
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
mShareActionProvider.setShareIntent(intent);
Edit:
The other option I would prefer is to ditch ShareActionProvider and use a normal menu item for this. The problem with ShareActionProvideris that you have to make the share Intent ready for user to share before hand. You have to make it ready even the user won't share it.
But when you have a normal button, it is much easier because you only make the operation when the user clicks the share button. In that case you can simply request the image one more time from Picasso with a Target object and write the Bitmap you got to a file in the shared external file system and share it.
I am using a bundle to send a bitmap from one application to another.
And i retrieve the bitmap from the bundle for use in a different application.
The specific use of bundle was necessary in this place.
And when i read it out i get a OUT OF MEMORY EXCEPTION.
bitmap = (Bitmap)receivedmsg.getData().getParcelable("myobject");
Any suggestions ?
It means what it says. The image you serialized is too big to be read back into memory in the other app. The fastest fix is what M Mohsin Naeem alludes to: you need to make the image smaller! Do so in the app that sends the image. For example, if that app is reading from a file, you can set it to down-sample the image to a smaller size.
Also consider whether you really need to send the image this way, or whether you could save it to the SD card, and then process it without reading into memory.
I've gone through this: How to send an object from one Android Activity to another using Intents?
But i'm facing problem in sending images in the object to the other activity.
Please help.
Convert your Drawable to Bitmap and send it to Another Activity.
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
To send,
intent.putExtra("Bitmap", bitmap);
To Fetch,
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
Well you can't really send images with intents since you can attach as extras to intents only Serializable objects.
But you can store images (in memory in different structures (like HashMaps, using hashmap will give you some speed optimization for searching that image)) and send notification to other activity to read from hasmap. You can add the key for the image in hashmap as string in extras attached to intent.
Or you can just cache the image and send it's name/path via intents :)
Hope this helps :)
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