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
Related
I have 3 images taken from camera and I want to set to 3 ImageView. But it throws OutOfMemory because the image has large size. I've read that bitmap factory can compress the size without reducing the quality. The problem is I don't know how to do that.
This is my onClick Button:
openCameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"imagename.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent,0);
dialog.cancel();
}
});
And this is my onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//result from camera
try{
if (requestCode==0 && resultCode==RESULT_OK){
//bitmap = (Bitmap)data.getExtras().get("data");
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outPutfileUri);
drawable = new BitmapDrawable(getResources(), bitmap);
imageSelfie.setImageDrawable(drawable);
}
} catch (Exception ignored){}}
Anyone can help me?
Use like this :
File file = new File(Environment.getExternalStorageDirectory(),"imagename.jpg");
fileDeleted = !file.exists() || file.delete();
if (fileDeleted) {
FileOutputStream out = new FileOutputStream(file);
if (imageToSave != null) {
imageToSave.compress(Bitmap.CompressFormat.JPEG, 65, out);
}}
out.flush();
out.close();
Instead of using the media store which tries to give you a full bitmap you better open an inputstream and load a resized bitmap from it.
InputStream is = getContentResolver().openInputStream(outPutfileUri);
Then use BitmapFactory.decodeStream() with an options parameter where you scale down the image several times.
I am trying to get image from gallery from my 1st Activity and want the resulted image in the ImageView of second activity.
Here is the Code For 1st Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageVew = (ImageView) findViewById(R.id.imageView);
}
public void useGalleryMethod(View view) {
//this is for picking Image from Gallery or file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
onActivityResult() in 1st Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
The Code For Second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Intent getIntentInfo = getIntent();
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = (Bitmap) getIntentInfo.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
}else{
return;
}
}
The App is running properly and showing the gallery image in ImageView of 1st Class Only and not going to 2nd Activity using the Intent in onActivityResult method.
Please Let Me know whats wrong with my code? ?
or is there any other way and I am not going in the right direction ?
Use this method which is more convenient that Parcelable you used:
method 1: Your can save that to sd card and retrieve.
OR
method 2:
Convert Bitmap to Byte Array:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Pass Byte Array with Intent:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Bitmap from ByteArray received in bundle:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
In onActivityResult() of first activity replace this -
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
// Send Image URI istead
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("imageUri", data.getData().toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
And on Second Activity do something like -
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(getIntentInfo.getStringExtra("imageUri")));
imageVew.setImageBitmap(bitmap);
}else{
return;
}
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
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);
}
}
I new in the android developing.
I want to develop simple application that will be able to take a picture using the cell phone camara and show it on the screen of the cell phone.
Is there some simple example that i can use ? or some code that can help me learn how to do it ?
Thanks for any help
to start camera you use
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
and here you have the handeling
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
Try this.. Use the below code in onCreate
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_RESULT);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Then OnActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//Here you will get path of image stored in sdcard then pass it to next activity as your desires..
mImagePath = extras.getString("image-path");
mSaveUri = getImageUri(mImagePath);
Bitmap mBitmap = getBitmap(mImagePath);
// here mBitmap is assigned to any imageview and you can use it in for display
}
}
private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = mContentResolver.openInputStream(uri);
return BitmapFactory.decodeStream(in).copy(Config.ARGB_8888, true);
} catch (FileNotFoundException e) {
//Log.e(TAG, "file " + path + " not found");
}
return null;
}
}