Android camera intent losing reference of Imageview in Lollipop - android

I have an activity in which i am calling intent of camera on click, but when I capture image and get back to the activity result my Image view reference is null. I am getting URI correctly. This happens in android Lollipop.
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
Log.i("dataa",String.valueOf(thePic));
Log.i("dataaimg",String.valueOf(image));
Log.i("dataaimgno",String.valueOf(imageno));
image.setImageBitmap(thePic); // here is the problem image reference is null when get back here
}
}
This code is working fine in versions below lollipop.

Try this:
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
image = setImageBitmap(bp);
}
}

Related

How to get image from camera

I have tried many codes from stackoverflow and internet.But I am able to find how to pick image from camera.I have used the following code,but data.getData() always returns null.Dont know how to solve.Pleas help this.
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, PICK_IMAGE_CAMERA1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 30 && resultCode == Activity.RESULT_OK&&data!null)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
Try this code #Thrishool,
private static final int CAMERA = 1;
//choosing the image from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
//now get the data from the onActivity result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
Log.e("savedImage",temp);
Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
}
}
Try this and let me know #Thrishool

How to set Image Button to some image in my internal storage (Android)?

Help me to set the uri of the image button to the image that i select form the internal storage. I have already given required permissions in the android_manifest.xml file. The problem is that when i browse to the internal storage of phone the images are unselectable.
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
Then in your on activity result method keep this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
EDIT
yes the problem is I forgot to add .Media
so use this line of code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
You can try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
String pathName = uri.toString();
Bitmap bmp = BitmapFactory.decodeFile(pathName);
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageBitmap(bmp);
}
}
Can you try this. You could replace ImageView to ImageButton
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);

Get full quality image from camera

When I take a picture with the camera and then I want to show this image in a ImageView, I was following the next method:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
I was getting the image and showing it in the ImageView correctly, but I have realized that the image obtained was of very low quality. After some research I found that the image obtained with this method is the thumbnail of the image taken. So I modified the code following some indications from other SO posts talking about this:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "Pictures/timeStamp.jpg";
takenPicUri = Uri.fromFile(new File(imageFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, takenPicUri);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
But now, the image is not being shown in the ImageView(pic_view). In the other posts I have read people report that this method worked for them, but is not working for me. I'm forgetting something or I'm doing something wrong?
Well, I have read in old posts that this way of doing this could cause trouble because there was some bug related to it but is the only way in that I have achieved to make it work.
Is as simple as this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Uri selectedImageUri = data.getData();
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
This way is working on android 5.0 and 4.4.4.

Bad image quality after picking an image from gallery

I use this to let the user pick an image from gallery:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
i.putExtra("crop", "true");
i.putExtra("scale", true);
i.putExtra("return-data", true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Then it will be displayed in an ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(photo);
}
}
It works but the picture now has a bad quality.
How to fix this?
You shouldn't specify any of those extras because they are device-dependent. Just do this:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
//load Uri into ImageView
}
}
I would look into using a library like Picasso to load the images instead of just doing imageView.setImageBitmap(photo) if your app is image intensive.

Captures the image from camera and load image into imageview of another activity

i am getting a problem in fetching an image captured by camera into image view of another activity so please, find out the problem.....
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
and the onActivityresult() is
protected void onActivityResult(int requestCode, int resultCode, Intent data,Uri mCapturedImageURI)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK)
{
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picturepath = cursor.getString(column_index_data);
Log.d("TAG", "getLastImageId::path " + picturepath);
Intent camintent= new Intent(MainActivity.this,GalleryActivity.class);
camintent.putExtra("imagePath",picturepath );
startActivity(camintent);
}
}
and my another(receiving) activity is........
private void getData(){
String ps=getIntent().getStringExtra("imagePath");
img.setImageBitmap(BitmapFactory.decodeFile(ps));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data,Uri mCapturedImageURI)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK)
{
Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
Intent camintent= new Intent(MainActivity.this,GalleryActivity.class);
camintent.putExtra("bitmap", bitmapImage);
startActivity(camintent);
}
}
And retrieve the Bitmap Image in second Activity
private void getData(){
Bitmap bitImage=getIntent().getParcelableExtra("bitmap");
img.setImageBitmap(bitImage);
}
Look here for an example on how to launch the camera and then retrieve the image:
Capture Image from Camera and Display in Activity
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);
}
}
If you want to pass the results between two different activities after receiving the image from the camera, you could save the bitmap as a file and then read it in the second activity.

Categories

Resources