1

I have a Listof File's in Android.

List<File> images

And I need to put that into a bundle. I have been trying to convert them to Parcelable but I have not found a way to do that.

bundle.putParcelable(BundleKeys.IMAGES.name(), images);

File comes from the camera pictures.

Richard
  • 1,087
  • 18
  • 52
  • It's considered a bad practice, bundles for Intent have a limit (1MB) for a reason, if you exceed it you'll get an exception. Consider using other more suitable storage types. Also read this: https://stackoverflow.com/questions/8552514/is-there-any-limit-of-bundle-in-android – Sergey Emeliyanov Dec 13 '19 at 12:25

3 Answers3

3

Use this for passing:

ArrayList<File> images; //Your list
bundle.putSerializable(BundleKeys.IMAGES.name(), images);

And use this for retrieving it:

ArrayList<File> images = (ArrayList<File>) getIntent().getSerializableExtra(BundleKeys.IMAGES.name());
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • Sorry for answering so late, but when I try to put the images into bundle I get this error: "Wrong 2nd argument type. Found 'java.util.ArrayList', required 'android.os.Parcelable'. When trying to put it like `(Serializable) images` I get a similar error. – Richard Jan 02 '20 at 07:21
  • 1
    Are you sure you're using `bundle.putSerializable()` and not `bundle.putParcelable()`? – Saurabh Thorat Jan 02 '20 at 07:37
  • Ahem.. Yes sorry, that was my mistake. One more question if you are so kind. I am not getting the `Serializable` throught intent but through `Bundle bundle`. So I am doing `bundle.getSerializableExtra(..)` but it says that it cannot `resolve method getSerializableExtra()`. What am I doing wrong there. – Richard Jan 02 '20 at 07:47
  • 1
    Bundle does not have `getSerializableExtra()`. Instead you can use [`getSerializable()`](https://developer.android.com/reference/android/os/Bundle#getSerializable(java.lang.String)) – Saurabh Thorat Jan 02 '20 at 08:41
2

java.io.File is a Serializable so that you could put it as Serializable. But, it is only for one file. However, it is not very efficient, and could cause ClassCastException, but you could probably cast your List to Serializable either.

bundle.putSerializable("IMAGES", (Serializable) images);
Oknesif
  • 526
  • 5
  • 11
0

Think if you actually need to pass whole File object. Maybe you can map some data and pass just them through Bundle.

Xirate
  • 109
  • 3