I have Activity1 which have one ImaveView including image preview. Once i press the button i go from Activity1 to Activity2. In Activity2 i do not have image preview but an option button "Save the image of Activit1?" YES or NO.
Currently i am doing in wrong way which is like saving the image in disk and then reading it back from disk. But is there any way without saving the image i can transfer one imageView from Activity1 to Activity2 ?
Here is how i get the picture in my Activity1 > ImageView, which then need to be moved to Activity2. Any idea?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.picture);
Button photoButton = (Button) this.findViewById(R.id.capture_btn);
Button btnShareToEmail = (Button) this.findViewById(R.id.btnshare);
btnShareToEmail.setOnClickListener(this);
//photoButton.setOnClickListener(new View.OnClickListener() {
//@Override
// public void onClick(View v) {
// Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(cameraIntent, CAMERA_REQUEST);
// }
//});
// without frozen
new Handler().postDelayed(new Runnable() { public void run() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}}, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
EDIT:
// Set - Activity1
Intent winShare = new Intent(getBaseContext(), Activity2.class);
winShare.putExtra("Title", "r2.jpg");
winShare.putExtra("image1", photo);
//int image_link = getIntent().getIntExtra("image1");
startActivityForResult(winShare,0);
// GET - Activity2
Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("image1"); // BITMAP_SHARED_KEY = "bitmap_shared_key"
imageView.setImageBitmap(bitmap);
// save it
imageView.buildDrawingCache();
Bitmap bm=imageView.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MYAPPPPPPPPPS" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
}