-1

Hello all my problem is that I am not able to transfer my images(Jpg) from one activity to other activity. I am stuck in my project in the middle So pls help me.

Activity1.java

public void Story1(View view) {

        String link=getResources().getResourceName(R.drawable.image1of1);
        Intent in = new Intent(Activity1.this,Activity2.class);
        in.putExtra("image",link);
        startActivity(in);
}

Activity2.java

protected void onCreate(Bundle savedInstanceState) 
{


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_cat1);

        i1 = (ImageView) findViewById(R.id.img1);

        Intent in = getIntent();
        Bundle b = in.getExtras();

        String img=getIntent().getStringExtra("image");
        i1.setImageURI(Uri.parse(img));




    }
Swapnil
  • 15
  • 4
  • 2
    Possible duplicate of [how do you pass images (bitmaps) between android activities using bundles?](https://stackoverflow.com/questions/4352172/how-do-you-pass-images-bitmaps-between-android-activities-using-bundles) – AskNilesh Oct 16 '17 at 09:55
  • 2
    passing byte array or bitmap itself is terrible idea (because of Intent size limit) `in.putExtra("image", R.drawable.image1of1);` and then `i1.setImageDrawable(ContextCompat.getDrawable(this, getIntent().getIntExtra("image")));` should do the thing – Selvin Oct 16 '17 at 10:05
  • getIntExtra() is asking for two parameters? – Swapnil Oct 16 '17 at 11:09
  • I have tried but not working – Swapnil Oct 16 '17 at 11:20

2 Answers2

1

Try to send id in integer form

Activity1.java

public void Story1(View view) {

    int link=R.drawable.image1of1;
    Intent in = new Intent(Activity1.this,Activity2.class);
    in.putExtra("image",link);
    startActivity(in);
}

Activity2.java

protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_cat1);

        i1 = (ImageView) findViewById(R.id.img1);

        Intent in = getIntent();
        Bundle b = in.getExtras();

        int img=getIntent().getIntExtra("image",-1);
        i1.setImageResource(img);
}
Andriy Kuba
  • 7,595
  • 2
  • 22
  • 42
Satender Kumar
  • 607
  • 4
  • 7
  • thanks brother it worked for me...But getIntExtra will take two arguments I have passed image and -1. and no function exists like that .getInt(). But thanks once again. – Swapnil Oct 16 '17 at 11:52
  • @Swapnil Second argument is the default value, if bundle doesn't contains "image" key then getIntExtras() will return that default value. Add one check before setting image resource i.e. if (img != -1) i1.setImageResource(img); – Satender Kumar Oct 16 '17 at 12:38
  • Andriy thx for the explaination I got what mistake i was doing but problem is solved now. – Swapnil Oct 16 '17 at 15:05
0

You need to send as byte array, to send,

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_name);     
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] data = os.toByteArray();

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("image", data);
startActivity(intent);

To receive in Activity2,

Bundle extras = getIntent().getExtras();
byte[] data = extras.getByteArray("image");

Then convert into bitmap in second activity,

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

Finally set into imageview,

imageView.setImageBitmap(bmp);

** *Try to send and receive using id

to send, get id using - getResources().getIdentifier(image name,"drawable", .getPackageName()); send - intent.putExtra("image",id);

to receive - intent.getIntExtra("image",-1); get drawable - getResources().getDrawable(id_in_activity2);

then set into imageview

Exigente05
  • 2,068
  • 3
  • 16
  • 35
  • not working I have tried your code, is this work for bitmap images only coz i have jpg images. – Swapnil Oct 16 '17 at 10:26
  • You should accept answer / upvote if it helps you bro. :) – Exigente05 Oct 16 '17 at 12:09
  • I have updated this answer 1 hour ago, someone answered at least 15 mins of that. This answer exactly solves your problem where another one have slight problem. Then why you didn't accept this? – Exigente05 Oct 16 '17 at 12:14
  • bro I have used ur second option without the byte array. and it worked for me. thx for the help and I have upvoted and accepted ur answer. I was too busy with the project thats y i was not replied so sorry for late response. – Swapnil Oct 16 '17 at 15:11
  • yeah i did it bro – Swapnil Oct 17 '17 at 21:32