0

I'm pretty new with android and I'm having problems specifying whether Intent is coming from camera or gallery in second activity. Here's my code:

MainActivity:

public class MainActivity extends ActionBarActivity implements OnClickListener {

private static final int CAMERA_REQUEST = 1888;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    Button cameraButton = (Button) this.findViewById(R.id.cameraButton);
    Button galleryButton = (Button) this.findViewById(R.id.galleryButton);

    //Here we choose to take a new picture
    cameraButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 

        }
    });

    // Here we choose a photo from the gallery
    galleryButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
        }
    }); 


}       

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

    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode)
    {
        case PICK_FROM_CAMERA:
            if (resultCode == Activity.RESULT_OK) 
            {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

                Intent captureIntent = new Intent(MainActivity.this, ImageViewerActivity.class);
                captureIntent.putExtra("data", imageBitmap);
                startActivity(captureIntent);

            }
            break;

        case PICK_FROM_GALLERY:
            if (resultCode == Activity.RESULT_OK)
            {
                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();

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

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

    public String getPath(Uri uri) {

        // just some safety built in 
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
    }

ImageViewerActivity:

public class ImageViewerActivity extends Activity {

public static ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.photo_view);

    imageView = (ImageView)findViewById(R.id.imageView);
    getData();

}

private void getData() {
    // TODO Auto-generated method stub

        if(//image is from camera)
        {
            Intent intent = getIntent();
            Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");
            imageView.setImageBitmap(bitmap);
        }


        if(//image is from gallery)
        {
            Bundle extras = getIntent().getExtras();
            byte[] byteArray = extras.getByteArray("picture");

            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            imageView.setImageBitmap(bmp);      
        }
}

Any help is needed! Thanks a lot!

  • What kind of problems? Please be more specific with your issues in order to make easier for us to help you – Juan Aguilar Guisado Nov 01 '14 at 10:26
  • I don't know how to tell getData() method whether image is from gallery or camera. As you can see right now if sentences are empty. – user10101 Nov 01 '14 at 10:30
  • Of course if there are better ways for passing photo intents than byteArray I'm open for everything. I just found this code somewhere and decided to give it a try. – user10101 Nov 01 '14 at 10:35

1 Answers1

0

In your extras, you can pass another field with "INTENT_FROM" and put a String with a identifier = CAMERA, GALLERY or whatever.

To attach it, before sending your intent, create a bundle and add it to the intent that will be sent, like this:

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

You can find tips to manage bundles here.

Passing a Bundle on startActivity()?

I hope this would help you!

Community
  • 1
  • 1
Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21