captured image quality reduced - android

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);

Related

How to save captured image without compress

I want to capture image and save it to storage. For storing am using below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
File root = new File(Environment.getExternalStorageDirectory(), "Feedback");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, Constants.PROFILE_IMAGE_NAME+".jpeg");
checkFlowIdisPresent(file);
Bitmap photo = (Bitmap)data.getExtras().get("data");
photo = ThumbnailUtils.extractThumbnail(photo, 300, 300);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
displayPic();
}
}
Now the problem is image quality is lost. So how to save an image without losing its quality.
if am using photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); to save image it doesn't losing its quality but the problem is i wanted to resize the image 300*300.
Please help me out.
User same code for capturing the photo. like -
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
selectedImageUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
startActivityForResult(intent, REQUEST_CAMERA);
// - selectedImageUri is global variable
now onActivityResult
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri );
after getting the result just resize your image using this method
public static bitmap resizeBitmap(Bitmap bitmap){
int width = 300; // - Dimension in pixels
int height= 300; // - Dimension in pixels
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
If you don't want any compression,then use direct bitmap what you are fetching from gallery for camera.Thanks
if(this.requestCode == requestCode && resultCode == RESULT_OK){
File root = new File(Environment.getExternalStorageDirectory(), "Feedback");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, Constants.PROFILE_IMAGE_NAME+".jpeg");
checkFlowIdisPresent(file);
Bitmap photo = (Bitmap)data.getExtras().get("data");
// use this bitmap as it is
}

Android - How to store and retrieve the image from internal storage

I want to take image from camera and store it and view it from internal storage.
I use this code to take image from camera.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PIC);
I tried the following to save the bitmap in internal storage.
public String saveToInternalSorage(Bitmap bitmapImage, String fileName){
File file = new File(createBasePath(), fileName + ".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return file.getName();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PIC && resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/png");
startActivity(i);
}
}
My path is /data/user/0/com.dev/app/1454351400000/33352/capture_Image1.png
But here I am facing one issue. The bit map size is very small. I want exact image what I taken from camera.
Then I found one solution. Set the path before intent. So I tried like this.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
output = new File(createBasePath(), "capture_Image1" + ".png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(intent, TAKE_PIC);
When I use this code the image is not showing. on onActivityResult the resultCode showing -1.
If I use the following code
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(intent, TAKE_PIC);
This code is working fine. After taking the image it showing image.
So please let me know store and retrieve the image from internal db with original quality.
Below is code to save the image to internal directory.
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Now Create imageDir
File mypath=new File(directory,"abc.jpg");
FileOutputStream foss = null;
try {
foss = new FileOutputStream(mypath);
// Using compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, foss);
} catch (Exception e) {
e.printStackTrace();
} finally {
foss.close();
}
return directory.getAbsolutePath();
}
Use below code for Reading a file from internal storage
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "abc.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
void openMultiSelectGallery() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri =getOutputMediaFile();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* start activity for result pass intent as argument and request code */
startActivityForResult(intent, CAMERA_REQUEEST_CODE);
}
/**
* This method set the path for the captured image from camera for updating
* the profile pic
*/
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "."
+ Constants.CONTAINER);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + System.currentTimeMillis() + ".png");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK
&& requestCode == CAMERA_REQUEEST_CODE) {
String path =fileUri.getPath();
//decode the path to bitmap here
}
}

How can i save the image taken from camera intent to be used as a profile image?

In my app the user can take an image from the camera and use as a profile picture. Everything works, except that when the user leaves the app and returns then the image isn't there anymore. So how can I save that image to stay there even when the user exits the app?
Code for camera intent:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
And onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
getLayoutInflater().inflate(R.layout.custon_header,null);
ImageView profimg = (ImageView) findViewById(R.id.roundedimg);
profimg.setImageBitmap(photo);
Could someone please assist me with this?
Thanks
data.getExtras().get("data");
only returns the thumbnail of the image which would be on low quality. You have to specify on your camera intent a location where the image will be saved:
File outputFile = new File(Environment.getExternalStorageDirectory() +"/myOutput.jpg");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(Intent.EXTRA_OUTPUT,outputFile.getAbsolutePath());
startActivityForResult(cameraIntent, CAMERA_REQUEST);
and on your activityResult, simply use the outputfile for your bitmap:
bitmap = BitmapFactory.decodeFile(outputFile.getAbsolutePath());
You can configure the intent to store the image, using EXTRA_OUTPUT like this:
Intent imageIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File imageFolder = new File(Environment
.getExternalStorageDirectory(), "YourFolderName");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
File imageFile = new File(imagesFolder, "user.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
imageIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
activity.startActivityForResult(imageIntent, CAMERA_REQUEST);
So you have the path and can decode the file to bitmap.
You need to save that image in sd-card or phone memory so that it can be accessed when you open your app again. You are seeing that image as soon as you take the image because it is there in the memory and as soon as you leave the app, the memory is released.
private void SaveBitmapInDirectory(Bitmap bitmap, String fileName)
{
File direct = new File(Environment.getExternalStorageDirectory() + "/dirName");
if (!direct.exists())
{
File profilePic = new File("/sdcard/dirName/");
profilePic.mkdirs();
}
File file = new File(new File("/sdcard/dirName/"), fileName);
if (file.exists())
{
file.delete();
}
try
{
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
And load the file from directory in onCreate function by calling the following function:
void loadProfilerImage(String fileName)
{
File file = new File(new File("/sdcard/dirName/"), fileName);
if (file.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView.setImageBitmap(myBitmap);
}
}

ImageView not displaying Image

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);
}
}

Android save camera's photo into internal memory

I am new to this site and android. how to save the camera's picture into specific folder and save the picture's name into sqlite databse also.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.camera);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
onActivityResult(1337, 0, cameraIntent);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == CAMERA_PIC_REQUEST) {
}
I opened camera activity. How to get the picture name & save it to particular location?
please help me on this.
thank you in advance.
I'm calling camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
and saving to sd card like this
final ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(100, 100));
image2 = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image2);
String incident_ID = IncidentFormActivity.incident_id;
//l2.addView(img);
imagepath="/sdcard/RDMS/"+incident_ID+ x + ".png";
File file = new File(imagepath);
try {
bm = Bitmap.createScaledBitmap( image2,400, 300, true);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(CompressFormat.PNG, 90, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"yourfirst error message is "
+ e.toString(), 1000).show();
}
You may need to change the line
if (resultCode == CAMERA_PIC_REQUEST) {
to
if (requestCode == CAMERA_PIC_REQUEST) {
You are already saving the picture. You provided the location in MediaStore.EXTRA_OUTPUT.
Link
Doing a DB you will have to have a look at this link is a little bit to much to explain

Categories

Resources