Putting an image to a Bundle - java

How can I save an image captured by the camera in a bundle and move it to the second activity?
Where am I doing wrong here?
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);
}
}
}

Bitmap implements Parcelable, so you should use:
Bitmap thumbnail = (Bitmap) data.getParcelableExtra("data");

There is a few way how to pass bitmap to second Activity
You can pass Bundle form Intent.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras()
Intent i = new Intent(this, PostActivity.class);
i.putExtra("extras", extras);
startActivity(i);
}
}
}
And in the PostActivity you can call
Bundle extras = getIntent().getBundleExtra("extras")
Bitmap thumbnail = (Bitmap) extras.get("data");
Or if you want to pass only image you have to convert Bitmap to byte array
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, PostActivity.class);
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("image",byteArray);
startActivity(i);
}
}
}
Or your method should also right. You have to get Bitmap by getParcelableExtra(String) method.
Bitmap thumbnail = getIntent().getParcelableExtra("data");

Related

Delete image in the gallery after camera intent photo taken

i'm creating an image cropping application and i want to delete the image automatically in the gallery after the camera intent photo taken.
my goal is to delete automatically the image taken from the camera app after cropping the image.
this is the code for opening camera
private void pickCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic");
values.put(MediaStore.Images.Media.DESCRIPTION,"Image to Text");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
startActivityForResult(cameraIntent,IMAGE_PICK_CAMERA_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_PICK_GALLERY_CODE) {
CropImage.activity(data.getData()).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
if (requestCode == IMAGE_PICK_CAMERA_CODE){
CropImage.activity(image_uri).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
}
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
mPreviewIv.setImageURI(resultUri);
BitmapDrawable bitmapDrawable = (BitmapDrawable)mPreviewIv.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if(!recognizer.isOperational()){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}

Android - How to pass a bitmap image from one activity to another activity without loosing image quality

I want to import an image from my mobile gallery using Button in Activity1 and display it in Activity2.
I'm using the following code to do so:
In MainAcitivity:
public void pickImageFromGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", false);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_PHOTO);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_PHOTO) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
Bitmap myPic = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myPic.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
}
}
In MainActivity2:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle extras = getIntent().getExtras();
ImageView imageView = (ImageView)findViewById(R.id.myImage);
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imageView.setImageBitmap(bmp);
}
I'm Importing high-quality image but getting very low Image Quality in ImageView. Can anyone tell me how to fix this problem?

How to call camera intent in CustomAdapter class in android?

I have Adapter class called CustomListAdapter class i written like this,so here i get a problem class cast exception Any one suggest me
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.myplace, null);
final ImageView ivCamera = (ImageView)convertView.findViewById(R.id.ivCamera);
ivCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("Do u Want to Take Photo");
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
((Activity) context).startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}
});
}
I get class cast exception please suggest me how to resolve this issue
in My Activity i call This method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
customListAdapter.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
ivCamera.setImageBitmap(bp);
}
Try this :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_CODE:
if(resultCode == Activity.RESULT_OK) {
customListAdapter.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
ivCamera.setImageBitmap(bp);
}
break;
}
}
http://developer.android.com/training/camera/photobasics.html
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
1) you have no check - probably onActivityResult() fires after some other activities started for result
2) resultCode == RESULT_OK && extras != null && extras.get("data") != null could be critical
3) try to run the receiving code in debug mode - some devices may not pass the bitmap (because it's very big, for example). if so - try to supply photoURI and then resize the photo before showing it
4) try to find libraries for getting photo
Yes, there are 2 types of Context in Android. 1 of them is Activity-type, the other - application Context.
Obviously, you should pass to adapter ref to your Activity, i.e. new Adapter(YourActivity.this), rather than new Adapter(getApplicationContext())
Answer the question - what activity should perform onActivityResult(), if you start activity for result using just getApplicationContext().startActivityForResult()? - And all become obvious.

Saving photo in my application

I'm trying to save a photo that I have already captured in my application (to use it later like as an icon for my contact) but I don't know if I must change it from uri to bitmap or any other type. I would like to save it as a .png like my others photos
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1020);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1020 && resultCode == RESULT_OK) {
mImageUri = data.getData();
Uri mImageUri2 = mImageUri;
Uri aux1=mImageUri;
Bitmap foto = photoUtils.getImage(aux1);
Drawable fotofinal =new BitmapDrawable(getResources(),foto);
Thanks for the help.

getting uri from android camera on all versions

I'm trying to take a picture with my app, save it and retrieve the url. Currently I have:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case TAKE_PICTURE:
{
if (resultCode == RESULT_OK)
{
File file = new File(getPath(data.getData()));
Date dt = new Date(file.lastModified());
String datum = dt.toLocaleString();
Comment_Resource photo = new Comment_Resource(file.getAbsolutePath(), datum, true);
}
break;
}
}
}
this works on some phones but a lot of phones don't save the picture causing a nullpointer.
try this:
Uri uri = Uri.fromFile(file);

Categories

Resources