0

i want to pass an image from one activity to another, this is my code:

 public boolean launchCamera(View view) {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo;
        try {
            // place where to store camera taken picture
            photo = this.createTemporaryFile("picture", ".jpg");
            photo.delete();
        } catch (Exception e) {
            Log.v(TAG, "Can't create file to take picture!");
            Toast.makeText(MainActivity.this, "Please check SD card! Image shot is impossible!", Toast.LENGTH_LONG);
            return false;
        }
        mImageUri = Uri.fromFile(photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
        //start camera intent
        this.startActivityForResult(intent, MenuShootImage);
        return true;
    }

private File createTemporaryFile(String part, String ext) throws Exception {
        File tempDir = Environment.getExternalStorageDirectory();
        tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
        if (!tempDir.exists()) {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }

 public Bitmap grabImage()
    {
        this.getContentResolver().notifyChange(mImageUri, null);
        ContentResolver cr = this.getContentResolver();
        Bitmap bitmap=null;
        try
        {
            bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);


        }
        catch (Exception e)
        {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "Failed to load", e);
        }

        return bitmap;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
         Intent imagepass = null;
         Bitmap bitmap = null;
         if(requestCode == MenuShootImage && resultCode == RESULT_OK)
         {

             bitmap = this.grabImage();
             imagepass = new Intent(this,MainActivity2.class);
             imagepass.putExtra("imagepass", bitmap );
             startActivity(imagepass);
         }

    }

the problem is that i cant reach to the other activity at all, in debug mode i get to the line startactivity(imagepass); and dont go to the MainActivity2. can somebody help me?

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
daniel gi
  • 396
  • 1
  • 7
  • 19
  • Have you declare MainActivity2 class in Manifest file? – Santosh Kathait Jun 23 '15 at 12:42
  • 1
    I will prefer to pass captured image path from one activity to another activity. In second activity you can create Bitmap from image path and can play with it. – Umesh Jun 23 '15 at 13:05

2 Answers2

1

First of all, if you really want to pass your bitmap through Intent, you need to convert it to Byte Array first since Bitmap couldn't be attached with Intent like that.

Here is how to do that https://stackoverflow.com/a/11010565/4651112

But according to the best practices, I suggest you NOT TO SEND A BITMAP THROUGH INTENT AT ALL. Send a filename image and let the target Activity decode it from file. It's much better.

Community
  • 1
  • 1
nuuneoi
  • 1,788
  • 1
  • 17
  • 14
0

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

Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16