I am trying to take a picture with the camera of "Nexus 5X API 26" and show it in ImageView field, before uploading it to Firebase.
First problem is that after taking a picture, it does not show up in imageView. I am doing the following:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);}
And then I was trying to show the picture in the same way I do for the pictures taken from Gallery:
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
The part which I do not understand is how data.getData() works in both cases (for Gallery and for Camera)?
I guess that the uploadImage() method, should be the same for both Gallery and Camera uploads (it works for Gallery already so...).
So the thing that I am currently missing is that I am not getting the filePath, I guess?
Is it necessary to "temporarily save" the camera's picture in order to .getData()? Or it can work without any kind of "saving"?
I just want the user to take a picture and it should be sent to Firebase. Not necessary for user to see it in imageView first, just to get the uri (data) in order to upload it.
view when i open the camera
view after taking a picture
This will help you.
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,7);
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
byte[] imageAsBytes = Base64.decode(imageEncoded.getBytes(), Base64.DEFAULT);
InputStream is=new ByteArrayInputStream(imageAsBytes);
bitmap1=BitmapFactory.decodeStream(is);
img.setImageBitmap(bitmap1);
}
Related
When I'm taking pictures using the camera functionality in my app, it's returning a low res image even though the camera preview is a good resolution. My code for calling the code is:
final Button takePicButton = (Button) findViewById(R.id.takePicButton);
takePicButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
});
My code for the activity result is:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Because (Bitmap) data.getExtras().get("data"); will return a thumbnail (low resolution) pic
Solution :
you need to use the URI mechanism (path to image file) and query the MediaProvider to fetch the full resolution image
Save the Full-size Photo
Starting the the camera with ACTION_IMAGE_CAPTURE , will always use the camera settings already saved on the camera, for example if the camera is setted out on low resolution (the user's config, use of another camera app ..) , even if you specify the uri you will get always low resolution.
So you have two options:
Build you own camera activity and set its setting to high resolution
Ask the user to set the camera resolution manually before starting the capture
Uri selectedImage = data.getData();
Bitmap picTaken = getBitmapFromUri(selectedImage);
use this function to get image from URI
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getActivity().getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor;
if (parcelFileDescriptor != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 50, out);
return BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
}
return null;
}
I am asking user to click image from my app and then that image gets stored in local storage and from there to the server ,but the problem is the image which is stored is blurred.
public void saveImage(Bitmap bitmap, int i)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100, bytes);
File directory = new File(Environment.getExternalStorageDirectory()+"my_directory");
if(!directory.exists())
{
directory.mkdirs();
}
File f = new File(Environment.getExternalStorageDirectory()+"my_directory");
try{
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
This is the code i am using to save image and this is the OnActivityResult from where the bitmap is generated.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
if(data!=null)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
iv_upload.setImageBitmap(thumbnail);
saveImage(thumbnail,(int)System.currentTimeMillis());
}
}
}
Is there any reason for compressing the image as done in your code?
The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data". The following code retrieves this image and displays it in an ImageView.
#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);
}
}
Have you made these changes in Manifest File, Please check
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
You are compressiong your original image here:
bitmap.compress(Bitmap.CompressFormat.JPEG,100, bytes);
Quality Accepts 0 - 100
0 = MAX Compression (Least Quality which is suitable for Small images)
100 = Least Compression (MAX Quality which is suitable for Big images)
So try to change the value of 100 and check the quality.
I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help
THanks
In onClick function I am using this code to pick image from gallery
Intent image = new Intent(Intent.ACTION_GET_CONTENT);
image.setType("Image/*");
startActivityForResult(image, 0);
And I have used following code to convert image that is in drawable to byte data.
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
data = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
bitmapdata = data.toByteArray();
Now how would i convert image from gallery to byte data.
Thanks
In onActivityResult you will receive the Uri to your selected image like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
Uri imageUri = data.getData();
//....
}
}
Then to retrieve it from the MediaStore you should use :
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
after that, you should process the Bitmap like you do it now.
In my application I capture photo by android camera and then I want to send it to the server. For this I use Client Socket programming. I convert the capture photo into bytearray(byte[]) and then send it to the server. Every thing work perfactally.
Problem is there I am not able to send original photo to the server. Thumbnail photo is sended by the android mobile phone. But when I capture photo by the camera then original photo is there in Gallery.
How to get this original photo to use in my application?
My Code:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG);
cameraIntent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(cameraIntent, "Select Picture"),
CAMERA_REQUEST);
And in onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// byte[] a = (byte[]) data.getExtras().getByteArray("data"); // I also use this but not work
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
}
}
in bitmapdata photo is there but compressed photo not the original.
Some of people say that, change quality field of compress(CompressFormat.JPEG, 100, bos); in between 0 to 100 but nothing will happen.
-- >> is there any other way also to get original photo which is captured by the camera
-- >> When The photo is in folder of my computer then I read this photo in the file by giving the path. ex- File file = new File(c:\newphoto\image.jpg); . can I use this code in android to read original photo because I know the name and location of photo. If yes then what is the path to read photo in gallery. Is this work if I give path as: \DCIM\Camera\photoName.jpg.
-- >> Or some change need in my current code and it will work fine?
You are using the Intent to capture the image using ACTION_IMAGE_CAPTURE. If you normally starts your camera using Intent then it will return image as a bitmap in onActivityResult() but it will be for thumbnail purpose.
If you want to get full resolution image from the camera then you should provide a file with the intent which you are firing to start the camera activity.
You can do like below
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
Here there is a MediaStore.EXTRA_OUTPUT parameter which takes a Uri of the file in which you want your camera to write a images.
For more information you can refer below example
Capture full resolution image from camera
I am using android inbuilt camera to take picture and then attaching the same picture to email, when i am testing this functionality in 1.6 device, i am able name the picture that to be taken by in built camera, but in 2.1, the picture is having a name i.e given by device,
How to give user defined name in 2.1 inbuilt camera images..
to avoid that problem i am saving the image manually but when i try to get the image back through intent as bitmap and then saving it to sd card using compress method
this methods handles result from inbuilt camera
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
File file = new File(Environment.getExternalStorageDirectory()
+ "/test.png");
switch (requestCode)
{
case PHOTO_ACTION:
if (resultCode == RESULT_CANCELED)
{
addPhoto = false;
Toast.makeText(this, "Canceled ", Toast.LENGTH_LONG).show();
break;
} else if (resultCode == RESULT_OK)
{
Bundle b = data.getExtras();
Bitmap bm = (Bitmap) b.get("data");
FileOutputStream out;
try
{
out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
scanPhoto(file.toString());
out = null;
addPhoto = true;
} catch (Exception e)
{
e.printStackTrace();
addPhoto = false;
}
but when i am storing like this i am getting two images. one with system given name and other with name given by me. but the image one which has user defined is having less resolution so i question is how to save the bitmap with more resolution with out compressing it ..
please help.... me
If you just want to save the bitmap without losing quality try using CompressFormat.PNG instead of JPEG, I've seen people having that problem before. Try changing:
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
with:
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
and see it it helps.
Apart from Rick answer above, make sure your are providing a URI to camera intent where it can save the full image, else it will return thumb image in data parameter of intent.
So it will be like:
Intent photoPickerIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File("Cache directory","img.png"); //== where you want full size image
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imgFile));
startActivityForResult(photoPickerIntent, PickPhoto);
This was the error that I was doing.