0

I'm trying to send an image from a activity to another, i was reading similar questions but none solved my problem. The code I am using to send this picture is

            public void aceptar (View view) {
            ImageView iv = (ImageView) findViewById(R.id.foto);
            iv.setImageBitmap(BitmapFactory.decodeFile(foto));
           File file = new File(foto);
            Intent intent = new Intent(this, XMPPClient.class); 
            ImageView img_view = (ImageView) findViewById(R.id.foto);
            img_view.setBackgroundResource(intent.getIntExtra("foto",1)); 
            startActivity(intent); 
            }   

and to receive the image

            Bundle extras = getIntent().getExtras(); 
            if (extras == null) 
            { 
                return; 
            } 
            int res = extras.getInt("resourseInt"); 

            ImageView foto = (ImageView) findViewById(R.id.foto); 

            view.setBackgroundResource(res); 

the error is this

   FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3606)
    at android.view.View.performClick(View.java:4211)
    at android.view.View$PerformClick.run(View.java:17446)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5336)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
Submersed
  • 8,810
  • 2
  • 30
  • 38

2 Answers2

0

This has been asked before but it's possible. Are you trying to pass an Image or ImageView? Both are possible

ImageView: Pass ImageView from one activity to another - Intent - Android

Integer.parseInt() only works for strings like "1" or "123" that really contain just the string representation of an Integer.

What you need is find a drawable resource by its name.

This can be done using reflection:

String name = "image_0";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier():

String name = "image_0";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Community
  • 1
  • 1
reidisaki
  • 1,525
  • 16
  • 29
0

This has been asked before, Passing image from one activity another activity There are 3 Solutions to solve this issue.

"1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

Convert Bitmap to Byte Array:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray();

Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class); intent.putExtra("picture", byteArray); startActivity(intent);

Get Byte Array from Bundle and Convert into Bitmap Image:-

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

2) First Save image into SDCard and in next activity set this image into ImageView.

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity."

Community
  • 1
  • 1
Matt T.
  • 539
  • 3
  • 6