Am trying to put the picture captured by the user in the second activity. Every time I capture the picture it takes me to the nextActivity but the problem am facing now is how to put the image captured inside the next activity so the user can see it
Please any one can guide me or direct me on how should i do it?
This is my code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Intent i = new Intent(this, PostActivity.class);
i.putExtra("name", thumbnail);
startActivity(i);
}
}
}
You can use following code to send data through Intent
Intent intent=new Intent(CurrentActivity.this,SecondActivity.class);
intent.putExtra("imagepath",path);
startActivity(intent);
Code To Receive Data that sent through Intent in SecondActivity
Bundle b=getIntent().getExtras();
String path=b.getString("imagepath");
Add an image path to intent extras and get it in the second activity.
Send Image URI using Intent Extras in between Activities.
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("uri",uri);
startActivity(i);
As you Receive Bitmap on onActivityResult method. so you can try with following code to pass image to theNextActivity.
1) Convert Bitmap to Byte Array
Bitmap mBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// Pass it to intent to send in NextActitivy
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("captured_image", byteArray);
startActivity(intent);
2) Get byte from bundle on NextActivity at onCreate() method
Bundle mBundle = getIntent().getExtras();
byte[] mBytes = mBundle.getByteArray("captured_image");
Bitmap mBitmap = BitmapFactory.decodeByteArray(mBytes, 0, mBytes.length);
ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
mImageView.setImageBitmap(mBitmap);
Related
I am not able to pass image to another activity as bitmap
Main activity
Intent intent=new Intent(LastActivityGrid.this,ActivityMetadata.class);
byte[] image = Utils.getImageBytes(gridlist.get(position).getBitmap());
intent.putExtra("image",image);
startActivity(intent);
second activity
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
Bitmap implements Parcelable, so you could always pass it with the intent:
Intent intent=new Intent(LastActivityGrid.this,ActivityMetadata.class);
Bitmap image = gridlist.get(position).getBitmap();
intent.putExtra("image",image);
startActivity(intent);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
If Image is not too bit then It will work.
But proper solution is to pass the actual location of the image and get the Image from there in the second activity. But As I understand you prefer quick solution.
I hope it will help!
Here's the Code to pass and get image through intent,
secondActivity
Intent intent = new Intent(this, PostVideoTagLocation.class);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.index);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent2 = new Intent();
intent2.putExtra("picture", byteArray);
startActivityForResult(intent,1);
setResult(RESULT_OK, intent2);
firstActivity
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.add_photo);
image.setImageBitmap(bmp);
}
}
Inside PostVideoTagLocation's onCreate method you can simply create a bitmap from the byte array that the creating activity has passed.
getIntent() method results in the intent that was used to create this activity, which will contain a bundle containing the picture bytearray.
However I am not completely sure how safe is it to store possibly big images inside an Bundle passed with Intent, so I would use this with caution.
You can use Intents getByteArrayExtra() with parameter "picture" which is used when you're setting the extra, and then simply with BitmapFactorys decodeByteArray decode it to an Bitmap.
Your code might looklike somethign like this
onCreate(Bundle bundle)..
byte[] rawBitmap = getIntent().getByteArrayExtra("picture");
Bitmap bitmap = BitmapFactory.decodeByteArray(rawBitmap)
and do with the bitmap whatever you want.
However I myself would save the image to a temporarily file and pass it with intent, just to be safe.
On your first Activiy you should call the second Activiy like this:
Intent intent = new Intent(PostVideoTagLocation.this, SecondActivity.class);
startActivityForResult(intent, 1111);
Then on Second Activity when you want to send image to first Activiy you should call:
Intent intent = new Intent();
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.index);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("picture", byteArray);
setResult(RESULT_OK, intent);
finish();
Finally in the first Activity write this code on onActivityResult:
if(requestCode == 1111){
Bundle extras = data.getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
mImageView.setImageBitmap(bmp);
}
It should work (I tested it). Hope it helps you!
[EDIT] You can save your bitmap to a File and just put the File path to the Intent
I need to pass image fetched from url into another activity page. I'm able to do so foe textviews. But i got stuck how to pass image from one page to another in android. dynamically through url.
can anyone give some suggestions regarding this.
1.First Way
Bitmap implements Parcelable
For Sent
Intent intent = new Intent(this, BActivity.class);
intent.putExtra("image", bitmap);
and Retrieve :
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
2.Second Way
//Convert to byte array for sent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
Then Retrieve in another activity:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
By using intent you can pass image url. from there you have to display that. You can not pass Image.
send data using intent
Intent openNewActivity = new Intent(getApplicationContext(), Activity2.class);
openNewActivity.putExtra("image", "www.fb.com/profilepic.png");
startActivity(openNewActivity);
get data from your Activity2.class file
write this code in your onCreate method
Bundle extras = getIntent().getExtras();
String image;
if (extras != null) {
image = extras.getString("image");
// and get whatever type user account id is
}
now set image url on Image.
I am using this cropper library. I want to know from the following chunk of code, how do I get the result of the cropped image's path in a string?
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, imageFilePath);
intent.putExtra(CropImage.SCALE, true);
intent.putExtra("outputX", 200); //Set this to define the max size of the output bitmap
intent.putExtra("outputY", 150); //Set this to define the max size of the output bitmap
intent.putExtra(CropImage.ASPECT_X, 0);
intent.putExtra(CropImage.ASPECT_Y, 0);
startActivity(intent);
First you need to change startActivity with startActivityForResult.
If you're done with that, in your activity, override onActivityResult, then do
String path = data.getStringExtra(CropImage.IMAGE_PATH);
where data is the Intent object from onActivityResult(int requestCode, int resultCode, Intent data)
As per the documentation at the libraries github page, you should be using startActivityForResult instead of startActivity.
By calling startActivityForResult, once that Activity has finished, onActivityResult will be called which will then allow you to pull the result out.
The following code is from the README.md of this library:
To start the activity:
private void runCropImage() {
// create explicit intent
Intent intent = new Intent(this, CropImage.class);
// tell CropImage activity to look for image to crop
String filePath = ...;
intent.putExtra(CropImage.IMAGE_PATH, filePath);
// allow CropImage activity to rescale image
intent.putExtra(CropImage.SCALE, true);
// if the aspect ratio is fixed to ratio 3/2
intent.putExtra(CropImage.ASPECT_X, 3);
intent.putExtra(CropImage.ASPECT_Y, 2);
// start activity CropImage with certain request code and listen
// for result
startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}
Waiting for a result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case REQUEST_CODE_CROP_IMAGE:
String path = data.getStringExtra(CropImage.IMAGE_PATH);
// if nothing received
if (path == null) {
return;
}
// cropped bitmap
Bitmap bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
I am trying to capture an image from an existing camera application, save the image in a customized folder, and display the thumbnail in and imageView. The camera supplies the thumbnail as long as I haven't specified where to save the file:
I can get the thumbnail from the returned intent:
...
Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i)
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
}
Or I can save the file in a specified folder (which works fine)
...
Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra((MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i)
}
but the thumbnail is no longer stored in the intent extra "data", and when I try to retrieve the thumbnail, I get an error (this is from my LogCat)
10-04 06:30:14.463: E/AndroidRuntime(1967): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity: java.lang.NullPointerException
As you can see, the field returned is null instead of the bitmap thumbnail. I have tried decoding the bitmap afterwards to generate a thumbnail from the file directly, but it takes too long (even when downsampled I get out of memory error) , and it seems counterintuitive to do the job twice. Any suggestions?
Okay. If you are passing an outputURI to the intent then you will not be able to receive the data back from the intent in onActivityResult().
I think only option is to use the same outputURI to display the thumbnail..
Try this.
void captureImage(){
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyFolder", "myImage"+ ".jpg");
mCapturedImagePath = file.getAbsolutePath();
Uri outputFileUri = Uri.fromFile(file);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, CAMERA_REQUEST);
}
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
File file = new File(mCapturedImagePath);
imageView.setImageURI(Uri.fromFile(file));
}
}
}
Your Bitmap mImageBitmap is a local variable, make that global if you want to use it outside the onActivityResultFunction otherwise set the image there as
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
try this
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);