-1

Is it possible to pass an Image as an extra to an activity?
For Strings it's working this way:

intent = new Intent(this, 2Activity.class);
intent.putExtra("String", "xy");
startActivity(intent);

So is it possible to do that for an Image? Or is the memory of the image a problem to do that?

mafioso
  • 1,480
  • 3
  • 18
  • 42
  • Possible duplicate of [how do you pass images (bitmaps) between android activities using bundles?](http://stackoverflow.com/questions/4352172/how-do-you-pass-images-bitmaps-between-android-activities-using-bundles) – StarWind0 Jan 03 '17 at 20:46

4 Answers4

3

There's a limit to the size of data passed via intent, so it probably won't work. The normal way to do this is to pass a filename or resourceID of the image.

Gabe Sechan
  • 77,740
  • 9
  • 79
  • 113
1

Good question. First, there's no Image class, so I'm not sure if that's something from a library you are including. Did you mean Bitmap?

The answer is in general no, you cannot. That code sequence is an inter-process call that passes the arguments through Android's binder kernel driver. There's a 1MiB hard limit on the size of the arguments. If you kept the image small it's possible, but consider that Bitmap is an uncompressed representation of the image so even what you might think is a reasonable sized image is going to be rather large in memory. Also consider that the 1MiB buffer is shared for all IPC to and from your process. If some other component of your app is performing IPC it will reduce the available buffer.

Consider passing a ParcelFileDescriptor that can be used to read the image data, a content:// URI pointing to a content provider that can serve the image data (see openFile()), or a file:// URI pointing to the image data on the file system.

Jeffrey Blattman
  • 21,054
  • 8
  • 74
  • 127
1

Sure you can. Either as a String, or create some sort of wrapper and implement parcelable... That said Bitmap is already Parcelable..

HOWEVER DO NOT DO THIS! This actually cost me a job interview once. As this question points out there is a 1MB limit. Which your image likely passes. What you Should be doing is saving it either as a file and passing the URI. Or storing the file at the application layer. Pass the URI, that is the clean way.

See this SO answer for bad way.

See this SO for URI.

Community
  • 1
  • 1
StarWind0
  • 1,479
  • 2
  • 15
  • 41
0

As other people had explained here, memory will be an issue. To resolve that you can store the images in a hashmap and pass the key around between fragments and activities.

You can use this wrapper class to hold the image for you in-memory.

public class DataWrapper {
private static final DataWrapper instance = new DataWrapper();
private final SparseArray<Object> map = new SparseArray<Object>();
private int currentKeyValue = 1;

public static DataWrapper getInstance() {
    return instance;
}

public int putData(final Object data) {
    ++currentKeyValue;
    map.put(currentKeyValue, data);
    return currentKeyValue;
}

public Object getData(final int key) {
    final Object ret = map.get(key);
    map.delete(key);

    return ret;
}

}

You can putData into wrapper like this:

  final byte[] imageData;
  final int imageDataKey = DataWrapper.getInstance().putData(imageData);
  data.putExtra(EXTRA_IMAGE, imageDataKey);

You can getData from wrapper like this:

final int imageDataKey = data.getIntExtra(CameraFragment.EXTRA_IMAGE, 0);
final byte[] data = (byte[]) DataWrapper.getInstance().getData(imageDataKey));
Dhareni
  • 1
  • 1