-3

I want to parse an image through intent extras, Intent successfully opens the second activity and parses the text fields data but not images:

Images are .jpg format if that helps.

i have used the below before on png images and seemed to work fine.

Here is First Activity:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                imageView.setDrawingCacheEnabled(true);
                bitmap = imageView.getDrawingCache(Boolean.parseBoolean(contactList.get(position).getProfilePic()));

                Intent intent = new Intent(MainActivity.this, DetailedActivity.class);
                intent.putExtra("imageView", bitmap);
                intent.putExtra("name", contactList.get(position).getName());
                intent.putExtra("email", contactList.get(position).getEmail());
                intent.putExtra("phone", contactList.get(position).getPhone().getMobile());

                startActivity(intent);

            }
        });

Second Activity:

public class DetailedActivity extends AppCompatActivity {

    //declared variables
    TextView txtName, txtEmail, txtPhone;
    Bundle img;
    String name;
    String email;
    String phone;
    ImageView imageView;
    Context mContext;
    Bitmap bitmap;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed);

        //Get Intent
        Intent i = getIntent();


        //items to receive intent
        name = i.getStringExtra("name");
        email = i.getStringExtra("email");
        phone = i.getStringExtra("phone");
        bitmap = i.getParcelableExtra("imageView");



        //Assign values to layout file items
        txtName = (TextView) findViewById(R.id.tvName);
        txtEmail = (TextView) findViewById(R.id.tvEmail);
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        imageView = (ImageView) findViewById(R.id.profilPic);



        /**
         * Set parsed Text
         */

        txtName.setText(name);
        txtEmail.setText(email);
        txtPhone.setText(phone);
        imageView.setImageBitmap(bitmap);
MinatoN3886
  • 11
  • 1
  • 2
  • Please explain, **in detail**, what "parse an image through intent extras" means. Please explain, **in detail**, what "but not images" means. Note that you are not using PNG or JPEGs here, but `Bitmap` objects. Also note that passing `Bitmap` objects via `Intent` extras is rather risky, as you have a limit as to how big an `Intent` can be. Instead of trying to get a `Bitmap` from the `ImageView` (which may or may not work), pass in the `Intent` information about the source of the image. – CommonsWare May 01 '17 at 13:08
  • Possible duplicate of [How can I pass a Bitmap object from one activity to another](http://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another) – Jitesh Prajapati May 01 '17 at 13:10
  • @CommonsWare, ok so i could pass the information about the source of the image i.e url, but how would i do that? – MinatoN3886 May 02 '17 at 11:02
  • Either your `ImageView` is empty, or it is not. If it is not, that is because you wrote some Java code to put something into the `ImageView`. That Java code is getting the image from somewhere: a resource ID, an HTTP URL, etc. Pass that data (the resource ID, the HTTP URL, etc.) to the other activity, so that it can use similar Java code. Bonus points for using [an image-loading library](https://android-arsenal.com/tag/46) that offers caching. – CommonsWare May 02 '17 at 11:54

1 Answers1

0

try this

first activity

bitmap = imageView.getDrawingCache(Boolean.parseBoolean(contactList.get(position).getProfilePic()));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

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

second activity

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);
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • I tried this but i am getting the below: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference – MinatoN3886 May 02 '17 at 10:52