1

I have problem with send image from imageView to another activity. My code works good but only for sending image given at code without changes. I add filters on the photo and I need to send image with this changes. This is my code:

First activity:

public void send(View view) {
    //trzeba tu coś wymyslić żeby dodawało np tag żeby wiedziec jaka obraz ma nazwe
    //i wstawić do tego niżej
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.i);     
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray();

    Intent intent = new Intent(this, TwoActivity.class);
    intent.putExtra("picture", b);
    startActivity(intent);
}

Next activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);

    Bundle extras = getIntent().getExtras();
    byte[] b = extras.getByteArray("picture");
    Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    image.setImageBitmap(bmp);
}

Please tell me what I should to change that it correctly send image with changes?

Quicki
  • 391
  • 1
  • 5
  • 15
  • Just pass in the changed Bitmap that you want to send to your send() method and use that Bitmap instead of decoding it from the resources (which will be the same thing as the unedited Bitmap) – Cruceo Mar 05 '15 at 22:09
  • Actually, if it's from the View, you may want to use the drawing cache... Making an answer for you... – Cruceo Mar 05 '15 at 22:10

1 Answers1

1

The reason it's the same is because you're only passing the image from the resources; not whatever is edited.

Since it sounds like you want to get the edited image from a View, you could easily get its drawing cache and use that.

public void send(View view) {
    Bitmap bitmap = getFromCache(view);     
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray();

    Intent intent = new Intent(this, TwoActivity.class);
    intent.putExtra("picture", b);
    startActivity(intent);
}

private Bitmap getFromCache(View view){
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); // Make sure to call Bitmap.createBitmap before disabling the cache, as the Bitmap will be recycled once it is disabled again
    view.setDrawingCacheEnabled(false);
    return bitmap;
}
Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • Can I use created DrawingCache in all my app or only on this one activity and should I send it by the intent to other? – Quicki Mar 05 '15 at 22:42
  • You can make it static and call that method from anywhere to get the drawing cache of any View. Just make sure it's always done on the UI thread (else you could run into ConcurrentModification issues) and that you only send it a View that is currently displayed (else you may just end up with an all-black image or a nice NPE). So yes, to answer your questions: Call that method in Activity A to pass the changed image to Activity B (as the View from Activity A displaying the edits could potentially not be available at that time Activity B tries to request it). Intents would be the way to go here. – Cruceo Mar 05 '15 at 22:47