0

I want to get the image from one activity to another activity. I tried converting the image to byte array in sender activity (PhotoActivity) and decoding the array to form original bitmap image on the receiver side (MainActivity) on the click of button 'btdn' but this isn't working. The image is visible in the sender activity's imageview but it stops when the button is clicked. This is my code:-

PhotoActivity.java

public class PhotoActivity extends Activity {
ImageView vitb;
Button btcm, btdn;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_IMAGE_SELECT = 2;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);
    vitb = findViewById(R.id.imageView);
    btcm = findViewById(R.id.btn_cam);
    btdn = findViewById(R.id.btn_back);

    btcm.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            selectImage();

        }

    });

    btdn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Drawable drawable = vitb.getDrawable();
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bs);
            byte[] byteArray = bs.toByteArray();
            Intent nIntent = new Intent();
            nIntent.putExtra("PICTURE", byteArray);
            setResult(RESULT_OK, nIntent);
            finish();
        }
    });

}

private void selectImage() {



    final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };



    AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);

    builder.setTitle("Add Photo!");

    builder.setItems(options, new DialogInterface.OnClickListener() {

        @Override

        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo"))

            {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);

            }

            else if (options[item].equals("Choose from Gallery"))

            {

                Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,REQUEST_IMAGE_SELECT);

            }

            else if (options[item].equals("Cancel")) {

                dialog.dismiss();

            }

        }

    });

    builder.show();

}



@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if (requestCode == 1) {
            Bitmap bmp;
            Bundle bundle = data.getExtras();
            bmp = (Bitmap)bundle.get("data");
            vitb.setImageBitmap(bmp);
        }
        else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

           //ImageView imageView = findViewById(R.id.imageView);
            vitb.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

}
}

MainActivity.java

public class MainActivity extends Activity{
private static final int REQUEST_CODE1 = 101;
private static final int REQUEST_CODE2 = 102;
Button btnLoc, btnPic;
Button btnSubmit;
TextView tvLoc;
ImageView image;
String phoneNumber = "9000000000";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSubmit = findViewById(R.id.btnSubmit);
    btnLoc = findViewById(R.id.btnLoc);
    btnPic = findViewById(R.id.btnPic);
    tvLoc = findViewById(R.id.textView3);
    image = findViewById(R.id.img);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                SmsManager.getDefault().sendTextMessage(phoneNumber, null, "Hello!", null, null);
            }
            catch (Exception e) {
                AlertDialog.Builder alertDialogBuilder = new
                        AlertDialog.Builder(MainActivity.this);
                AlertDialog dialog = alertDialogBuilder.create();
                dialog.setMessage(e.getMessage());
                dialog.show();
            }
        }
    });

    btnLoc.setOnClickListener(new View.OnClickListener()
    {
         @Override
         public void onClick(View view) {
             Intent intent = new Intent(MainActivity.this,LocActivity.class);
             startActivityForResult(intent,REQUEST_CODE1);
         }
    }
    );

    btnPic.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this,PhotoActivity.class);
            startActivityForResult(intent,REQUEST_CODE2);
        }
    });

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        if(requestCode == REQUEST_CODE1 && data !=null) {
            String strMessage = data.getStringExtra("loc");
            tvLoc.setText(strMessage);

        }
        if(requestCode == REQUEST_CODE2 && data !=null) {
            Bundle extras = getIntent().getExtras();
            byte[] b = extras.getByteArray("PICTURE");
            Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
            image.setImageBitmap(bmp);

        }
    }

}

}
Henodi
  • 75
  • 1
  • 1
  • 10
  • try [this](https://stackoverflow.com/a/2459624/4778343) – Stefan Golubović Mar 25 '18 at 12:35
  • But it is passing only those images taken from camera not those that are picked from gallery. – Henodi Mar 25 '18 at 13:32
  • Just one question: At your btdn onclick, "bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bs);" why your quality value is zero? It may lead your data is empty? Didn't try before but you may check it. – Oğuzhan Döngül Mar 25 '18 at 14:27
  • Actually, the code is working fine now. The only change needed was in the onActivityResult() part of main activity where getIntent().getExtras() should have been data.getExtras(). PS: that quality value doesn't nullify the data completely, it just deteriorates the image. @OğuzhanDöngül – Henodi Mar 25 '18 at 14:40

0 Answers0