0

I have two classes. i want to pass array of image paths from activity to another activity. How can i do that.These images are stored in sdcard. I am able to retrieve the single image in the form of inputstream and convert that into bytearray and passing to another activity through an intent. But how can i do that for array of images.

Please see the code i used for converting from inputstream to bytearray.

  istr = getExpansionFile().getInputStream("data/image1.png");
  Bitmap bitmap = BitmapFactory.decodeStream(istr);
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();
  intent.putExtra("image",byteArray);
  startActivity(intent);
user1903270
  • 77
  • 3
  • 14
  • Singleton class seems to be the solution : http://stackoverflow.com/questions/4878159/android-whats-the-best-way-to-share-data-between-activities – kmas Jul 05 '13 at 21:16
  • Image paths are something *very* different from the actual image bytes. If it's the former, then simply populating a list of strings and passing them on using `putStringArrayListExtra()` will do. If it's the latter, then you should consider reloading them in the next activity. From a memory point of view, passing around a potentially large number of bytes is simply not a viable option. – MH. Jul 05 '13 at 21:55

3 Answers3

2

I had to do the same in my application Pic4Share and I solved it in this way:

From the first activity:

String sPath[] = new String[MAX_PICS]; // This is the array that has the paths stored 

Intent myIntent = new Intent(Activity1.this, Activity2.class);
Bundle b = new Bundle();
for(int i=0; i<sPath.length; i++){
    b.putString("sPath" + Integer.toString(i), sPath[i]);
}
b.putInt("iLength", sPath.length);
myIntent.putExtras(b);
startActivity(myIntent);

And from the second activity you get back the paths in this way in onCreate:

String sPath[] = new String[MAX_PICS]; // This is the array where the paths are being stored

Bundle b = getIntent().getExtras();
iLength = b.getInt("iLength");
for(int i=0; i<iLength; i++){
    sPath[i] = b.getString("sPath" + Integer.toString(i));
}

Now that you have the paths again then you can decode the bitmaps!

Julian Mancera
  • 194
  • 1
  • 12
1

In general this is just a bad idea, passing images between activities is prone to crash on devices with low memory and is also unnecessary, why don't you just pass the path to the image and have the new activity also load them? Or save the image to the mediastore and pass a reference to the new activity

Ryan S
  • 4,549
  • 2
  • 21
  • 33
0

Cache your images from the first activity and reuse them from others.

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28