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.
Related
Using following code to view captured image in an ImageView
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK) {
File imageFile = new File(Environment.getExternalStorageDirectory() + "/img.jpeg");
Picasso.with(MainActivity.this).load(imageFile).into(imageView);
}
}
Problem:
How Can I refresh ImageView in onActivityResult(...) as you can see I am using above code but always getting preview of old image, whereas i am getting new image inside storage
You can replace the code these lines of code for it:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK) {
File imageFile = new File(Environment.getExternalStorageDirectory() + "/img.jpeg");
Picasso.with(MainActivity.this)
.load(imageFile)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.error(R.drawable.ic_lanucher)
.noFade()
.into(imageView)
}
}
If you want to load the captured image, you need to get bitmap from data i.e. your Intent object.
if (responseCode == RESULT_OK && data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
if (bitmap != null) {
// Here you will get bitmap , set bitmap in imageview
}
}
I am trying to give the user the option to either choose an image from the phone's storage or take one with the phone's camera app and then display that image in an imageView. With the help of this and this question I am able to display the image when it is choosen from the storage. However if I choose to take a picture with camera although I don't get an error the image is not shown in the imageView.
The function for choosing the picture looks like:
public void choosePic(View view) {
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
}
After that it is handled by
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
EDIT
With #Murtaza Hussain's helps I was able to find a working solution:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("requestCode",""+requestCode);
if(requestCode == PICK_IMAGE && resultCode==RESULT_OK && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
else if(requestCode== PICK_IMAGE && resultCode==RESULT_OK && data.getData() == null){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
super.onActivityResult(requestCode, resultCode, data);
}
You need to add code for it in onActivityResult()
if(requestCode==0 && resultCode==RESULT_OK ){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
In your activity when to capture image from camera. write the below code.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/imageLarge"+System.currentTimeMillis()+".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, 111);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
In onActivityResult() write the below code..
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 111:
if(resultCode == RESULT_OK)
{
Log.v("","After camera capture path is --> "+mImageCaptureUri.toString().substring(8));
Bitmap bitmap=BitmapFactory.decodeFile(mImageCaptureUri.toString().substring(8));
image.setImageBitmap(bitmap);
}
else
{
}
}
}
Do not forget to add one permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In case of any query, Please let me know. Thanks.
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.
i am implemented this code image upload device only how can retrieve bitmap uri
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ACTIVITY_RESULT_IMAGE_SELECTED);
protected void onActivityResult(int requestCode, int resultCode, Intent intent,Intent data, CharSequence Uri)
{
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == ACTIVITY_RESULT_IMAGE_SELECTED)
{
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
Object mBitmap = MediaStore.EXTRA_OUTPUT; //Media.//getBitmap(this.getContentResolver(),uri);
//EditText images=(EditText) findViewById(R.id.EditTextImages);
// mImageUploadAdapter.addImage(uri);
// mImageUploadGridView.invalidateViews();
}
} i am implementing this code
intent.getData() will return the URI of the Bitmap. If your question is about how you are to create a Bitmap Object from this URI you can take a look here.
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);
}
}