I am using camera in my app.I need to take picture and should display it in imageview. I am using the following code to take picture from the camera and display it.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(intent, 0);
imageView.setImageURI(capturedImageUri);
This works only for two or sometimes three images,then the imageview doesn't show the image but the image is correctly stored in SD card.
Alternatively i have also used
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
Imageview.setImageBitmap(bitmap);
But i am facing the same problem. can any one help me please.
capturedImageUri will return path to captured Image not the actual Image..
Also, Important Note-- If you dont need a full sized image use-
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); Comment this line
Bitmap image = (Bitmap) data.getExtras().get("data");
To get the full sized Bitmap Use following code-
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try
{
// place where to store camera taken picture
tempPhoto = createTemporaryFile("picture", ".png");
tempPhoto.delete();
}
catch(Exception e)
{
return ;
}
mImageUri = Uri.fromFile(tempPhoto);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(cameraIntent, 1);
private File createTemporaryFile(String part, String ext) throws Exception
{
// File to store image temperarily.
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists())
{
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver cr = getApplicationContext().getContentResolver();
try {
cr.notifyChange(mImageUri, null);
File imageFile = new File(tempPhoto.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Bitmap photo=null;
if (resultCode == 1) {
try {
photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
imageView.setImageBitmap(photo);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Have you tried putting your setImageUri logic in the activity callback onActivityResult
I think you are trying to supply the image to the imageview before any content is saved to that uri. You need to hook into the callback that gets fired when the camera actually take the picture (i.e. the camera activity finishes and reports its result)
This answer has a full write up
https://stackoverflow.com/a/5991757/418505
possibly something like
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);
}
}
Related
The picture captured using camera in my application is saved in a folder in External Storage.When I check the folder I found out that it's quality is low compared to the same image in the gallery.Using FileOutputStream I save the image to the folder.Is there any other way where I could save the image without losing the quality?Below is my code:
Code to save captured Image to Folder:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==2&&resultCode==RESULT_OK)
{
File camerafile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/CameraTestFile");
if (!camerafile.exists())
{
camerafile.mkdir();
Toast.makeText(getApplicationContext(),"Folder created",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Folder already exists ",Toast.LENGTH_SHORT).show();
}
Bitmap bitmap;
bitmap= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
try {
FileOutputStream fileOutputStream=new FileOutputStream(camerafile+"/camera3.png");
fileOutputStream.write(byteArrayOutputStream.toByteArray());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Refer to my answer on https://stackoverflow.com/a/50929913/9882512>
You can use the cod. Just change the path of file. It will directly save the file to the desired location and there will be no need to copy the file
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String pictureName = getPictureName();
imagefile = new File(pictureDirectory, pictureName);
picUri = Uri.fromFile(imagefile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
I've been developed app to take a picture to be saved in gallery. I've searched online and the easiest way that I could practice was to use data.getExtras().get("data") . So below are the code to take picture from camera. Note that I'm using fragment in this class.
img22a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_01);
}
});
Get the captured image and convert it to string
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_01) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] byte_arr = output.toByteArray();
((SiteProgress) getActivity().getApplication()).seti22a1(Base64.encodeToString(byte_arr, Base64.DEFAULT));
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
img22a1.setBackgroundDrawable(ob);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
I save the string into global variable so another activity can access it.
In another activity, I have a button to save the image into folder/gallery by accessing the image string.
for(int i=0;i<=namefile.length;i++){
Bitmap[] bitmap = new Bitmap[namefile.length];
FileOutputStream outputStream = new FileOutputStream(String.valueOf(namefile[i]));
bitmap[i] = BitmapFactory.decodeByteArray(decodedString[i],0,decodedString[i].length);
bitmap[i].compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), namefile[i].getAbsolutePath(), namefile[i].getName(), namefile[i].getName());
}
It worked to save the image into folder/gallery....But the quality of image was very low...I could barely see text in the image.
They said data.getExtras().get("data") supposed to return thumbnail and not the actual image. What I want here is to get the full image and not the thumbnail one.
Any answer will be very appreciated.
From: Android - How to get the image using Intent data
if(data != null)
{
Uri selectedImageUri = data.getData();
filestring = selectedImageUri.getPath();
Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);
System.out.println(String.format("Bitmap(CAMERA_IMAGES_REQUEST): %s", thumbnail));
System.out.println(String.format("cap_image(CAMERA_IMAGES_REQUEST): %s", cap_image));
cap_image.setImageBitmap(thumbnail);
}
I have a problem that occurs when I click an image using Intent and launch Android Camera. The image that I get through Intent data carries information of resized Bitmap image. Maybe I have a wrong understanding, but please suggest what can I do to correct it. The ImageView displays the same image I clicked but a very blurrred one
Here is the underlying code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream= null;
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
try{
//stream= getContentResolver().openInputStream(data.getData());
Bundle extras= data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
imageHolder.setImageBitmap(bitmap);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
try{
//stream= getContentResolver().openInputStream(data.getData());
Bundle extras= data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
//set value Width=200, Height=200
Bitmap resizedimg = Bitmap.createScaledBitmap(bitmap, Width, Height, true);
imageHolder.setImageBitmap(resizedimg);
}
catch (Exception ex){
ex.printStackTrace();
}
maybe you should pass a uri to Camera, and get image from it when ActivityResult instead of bitmap
try this:
public class MainActivity extends Activity {
static int CAMERA_TAKE_PIC =1;
Uri outPutfileUri;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
}
public void CameraClick(View v) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"Photo.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, CAMERA_TAKE_PIC);
}
Bitmap bitmap = null;
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
if (requestCode == CAMERA_TAKE_PIC && resultCode==RESULT_OK) {
String uri = outPutfileUri.toString();
Log.e("uri-:", uri);
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
//Bitmap myBitmap = BitmapFactory.decodeFile(uri);
// mImageView.setImageURI(Uri.parse(uri)); OR drawable make image strechable so try bleow also
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
Drawable d = new BitmapDrawable(getResources(), bitmap);
mImageView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
i think the image is blurred because the intent data contains only the thumbnail of the image.you can save the image on the internal/external storage and then acquire it.
The image is probably blurred because the intent data contains only the thumbnail of the image. You must save the image on the internal/external storage and then acquire it.Also your question should be how to get full size image from camera.
Go through this doc https://developer.android.com/training/camera/photobasics.html.
This is how it should be done
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
Refrence can be found in this link.
https://stackoverflow.com/a/32121829/3111083
I'm trying to set the thumbnail of the picture taken to the scr of an ImageButton. The following code works fine with taking the picture and storing it in the gallery but the "Bitmap imageBitmap = (Bitmap) extras.get("data");" returns null.
Can anyone please explain why?
private void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i("Camera log", "Failed:" + ex.toString());
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
#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");
cameraBtn.setImageBitmap(imageBitmap);
galleryAddPic();
}
}
When you set a MediaStore.EXTRA_OUTPUT you have to get the photo from this URL.
Example:
if(photoFile!=null){
BitmapFactory.Options bmOptions = null;
bmOptions = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeFile(photoFile.getPath(), bmOptions);
}
else{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
cameraBtn.setImageBitmap(imageBitmap);
galleryAddPic();
}
Can anyone please explain why?
You provided EXTRA_OUTPUT. Your image should be stored at the location you specified. If you do not provide EXTRA_OUTPUT, then extras.get("data") should have your Bitmap (at least if the camera app your app winds up using does not have bugs).
I have problem with intent in android. I use intent transfer image have been made by camera (camera of device) to bitmap, and then I show it. But it's too small. My camera is 8mpx.
so why and how can I fix it?
Could you be more specific, Is the photo taken directly from camera or selected from gallery?
Most probably the size of the imageView itself is small. A photo no matter how small can be enlarged to fit a large imageView but it will pixelate.
If you have its URI you could try this code to convert data from intent to uri which then gets converted to bitmap and then assigned to your imageView
Uri imageUri = intent.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Imageview imgview1 = (Imageview ) findViewById (R.id.imgview1);
imgeview1.setImageBitmap(bitmap);
You could use the above code to convert URI recieved from camera to a bitmap and assign it to the imageView.
If the image still looks small then check the size of the imageView.
private static final int TAKE_PICTURE = 1;
private Uri imageUri;
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
}
Please follow the instruction of below link http://developer.android.com/guide/topics/media/camera.html