I'm using camera intent to capture image inside my android app. After capturing I save pics into mobile internal/External storage in a specific folder. The problem is that these pics are not being saved in that resolution which camera has normally, their resolution is very low.
here is my code
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
/*********** Load Captured Image And Data Start ****************/
String extr = Environment.getExternalStorageDirectory().toString()
+ File.separator + "ScannerMini";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
imageName = timeStamp + ".jpg";
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
myPath = new File(getExternalFilesDir(filepath), imageName);
//File myPath = new File(extr, imageName);
}
else{
myPath = new File(extr, imageName);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getApplicationContext()
.getContentResolver(), bp, myPath.getPath(), imageName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
can anybody tell me that what should i do to save image in original resolution. Any help would be much appreciated. Thank You :)
Bitmap bp = (Bitmap) data.getExtras().get("data");
By doing this you will only get a thumbnail image. You need to specify MediaStore.EXTRA_OUTPUT option in your capture intent. This is a path where captured image will be stored. Refer to android docs Taking Photos Simply
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);
Getting image Content from Gallery picture
Now, I want to copy this image file(Which is picked from gallery) to my Internal App folder PICTURE directory(/storage/emulated/0/Android/data/MYApppackage/files/Pictures/).
I am creating new File in PICTURE Directory. file is created in internal AppDir / data / Pictures Directory and image with .JPG format but not showing image. Image is not visible. Image file is created with 0KB data.
I think its a file writing related issue or other i dont know.
Intent intent ;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
MainActivity.state = "AddBalance";
startActivityForResult(intent, REQUEST_GALLERY);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_GALLERY);
}
OnActivity Result of Gallery PickUp
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_GALLERY:
if (data != null) {
if (data != null) {
Bitmap bitmap=null;
uri1 = data.getData();
// showPhotoDialog(uri1);
try {
// bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri1);
InputStream image_stream = getActivity().getContentResolver().openInputStream(uri1);
bitmap= BitmapFactory.decodeStream(image_stream);
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG,"Data URI----"+data.getData());
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "PNG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".PNG", // suffix
storageDir // directory
);
// File imageFile= new File(storageDir,imageFileName);
FileOutputStream out;
try {
// out = getActivity().openFileOutput(image.getName(), Context.MODE_PRIVATE);
out = new FileOutputStream(image.getName());
/* ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
out.write(byteArray);*/
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (Exception e) {e.printStackTrace();}
// copyFile(new File(getRealPathFromURI(uri1)), image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`
}
Edit : Infos
getting value of uri1 from pickup gallery image : content://com.android.providers.media.documents/document/image%3A25877
I am Trying to write bitmap to File. Here is my Code. getBitmap from REQUEST_GALLERY and URI works fine . I am making File in Enviroment.PICTURE directory and write via OutPutStream. but Still File : PNG_54342323111.PNG
I also tried with ByteArrayOutputStream which you can see in comments but didnt works. also tried with OpenFileOutput but not working.
I want to take a picture and save it as a file on the sd-card. All works fine, the camera starts, the pictures were taking, and saving. If i check the picturefolder on my device, i see the taken picute, but if i check the folder from another actvity, i cant see the taken pictures. The same, if i check the folder from my pc. What is wrong with my code?
Here is my code for the Cameraactivity
//Init ImageView
mPhotoCapturedImageView = (ImageView) findViewById(R.id.imgViewThumbNail);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, ACTIVITY_START_CAMERA_APP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mPhotoCapturedImageView.setImageBitmap(thumbnail);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMAGE_" + timeStamp+ "_";
File file = new File(Environment.getExternalStorageDirectory()+ PHOTO_ALBUM + imageFileName + ".jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The path of PHOTO_ALBUM is
public static final String PHOTO_ALBUM = "/MyIdea/IdeaGallery/";
I found out, that SD-Card is scanning after mounting and show all new pictures. This simply line of code solve all my problems:
MediaScannerConnection.scanFile(CameraActivity.this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
Thanks all for the help!
I my developing an app that clicks the photos and it should store the photos in a different folder than the default storage location. please guide me about it.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
This is the basic code, please tell me the changes needed to make in it.
You cannot change default folder location, but you can make copy of image for your use. If you want to change it, then play with system camera.
You Need to call on activity result and get captured image and store it on any location.
For my case file(image) name is currenttimestamp.jpg
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK){
try {
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File tmpFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
FileOutputStream fos = new FileOutputStream(tmpFile);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
EDIT
tell android you have copied the picture. This will make the folder show up as a gallery on the users device just pass in the path and mimetype to a media scanner connection.
conn.scanFile(fos..getAbsolutePath(),"image/*");
**End edit *
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*Edit setup the mediascanner connection *
private static MediaScannerConnection conn;
somewhere in onCreate()
conn = new MediaScannerConnection(this, this);
conn.connect();
#Override
public void onScanCompleted(String path, Uri uri) {
// now is good time to save stuff in the database because
// the last segment of the uri is the imageId in the mediaStore.
// you can easily pull a thumbnail from the mediaStore with the imageId.
// example of getting thumbnail
//final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// bmOptions.inSampleSize = 1;
//Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
// context.getContentResolver(), newImageId,
// MediaStore.Images.Thumbnails.MINI_KIND,
// bmOptions);
}
#Override
public void onMediaScannerConnected() {
//toast ready
// enable your camera button
}
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyProjectName");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();// <----Do Not Forget this
}
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmmss_Z");
String currentDateandTime = sdf.format(new Date());
String fileName = "image_" + String.valueOf(currentDateandTime) + ".jpg";
File output = new File(imagesFolder, fileName);
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved: ",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
I have captured image through camera intent and image are saved to internal memory.But my images are also stored in gallary .I don't want to save the images into gallary for private purpose.
Here is my code:-
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);#SuppressLint("NewApi")
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
FileOutputStream mFileOutStream1;
try {
mFileOutStream1 = openFileOutput("IMG" + counter + ".png", Context.MODE_PRIVATE);
photo.compress(CompressFormat.JPEG, 100, mFileOutStream1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this code
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
Hope this help you..!!!