I am making an app in which i can pick an image from gallery or i can take image from camera and i am moving to next activity with the selected image.There I want to distinguish whether the image is coming from gallery or camera.I am not getting any idea how to do it.I am providing my code.Please help me how to do this.
My first activity is
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class LauncherActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private static int RESULT_LOAD_IMAGE = 1;
ImageButton camera;
ImageButton gallery;
ImageView gallery_image;
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);
camera = (ImageButton)findViewById(R.id.take_photo);
gallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);
}
});
camera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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();
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
intent.putExtra("path", picturePath);
startActivity(intent);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data!=null) {
Bitmap photo_from_camera = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo_from_camera.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
intent.putExtra("image", byteArray);
startActivity(intent);
}
}
}
and my second activity is
public class MainActivity extends Activity
{
Bitmap scaled;
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
if(//condition to The image coming from gallery)
{
Bundle extras=getIntent().getExtras();
String picturePath=extras.getString("path");
scaled=BitmapFactory.decodeFile(picturePath);
//the things i have to do with bitmap that i know
}
if(//Condition to the image coming from camera)
{
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("image");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
scaled = Bitmap.createScaledBitmap(bitmap, width, height, true);
//the things i have to do with bitmap that i know
}
}
}
Here I am not getting the code to write the conditions in the if statement.please help me.Thanks in advance.
regarding to your request code you can differentiate image is coming from gallery or camera
in second activity you can get boolean from intent.if it is true image from gallery