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
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 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 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
}
I'm trying to upload an image to server in my Android application by converting it to a base64 string. In this case when I try to upload an image taken from my camera the quality of my image is largely reduced and is very much blurred. Can you please help me overcome this issue.
Here's my code:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Log.i("camera intent", "camera intent");
bitmap = (Bitmap) data.getExtras().get("data");
bitmap = Bitmap.createScaledBitmap(bitmap, 720, 1280, true);
viewImage_imageView.setImageBitmap(bitmap);
UploadImage_textView.setEnabled(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
String file = Base64.encodeToString(data, 0);
Log.i("base64 string", "base64 string: " + file);
new ImageUploadTask(file).execute();
}
}
bitmap = (Bitmap) data.getExtras().get("data");
This will give the thumbnail of the image taken by camera that's why you are seeing blurred image
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri tempURI = Uri.fromFile(<file path where you want to save image>);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
then for this path you will get the actual image click by camera now upload that image to server.
I am trying to capture an image using Android Camera Intent. Camera intent returns Byte Array and when I saved the byte array as a Bitmap, I am getting a very small image instead of getting an Image based on current camera settings (1024 Pixels currently set in the android mobile camera).
Usally i will get file path from the camera intent but somehow i am not getting from this device, so i am creating the bitmap from the byte returned by camera intent.
Anybody knows why is this and how to solve this issue. Thanks.
The below is the java code block I am using.
private Intent cameraIntent = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
if ( data != null)
{
Bitmap myImage = null;
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
byte[] byteArray = stream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
myImage = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length, options);
fileOutputStream = new FileOutputStream(sPath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}
}
}
}
The data.getExtras("data") only returns a thumbnail image. To obtain the full-sized image, you need to pass the camera intent a file in which to store that image and later retrieve. A rough example follows.
Start the intent:
File dir = new File(Environment.getExternalStorageDirectory() + "/dcim/myappname");
File mFile = File.createTempFile("myImage", ".png", dir);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Inside onActivityResult:
if (resultCode == RESULT_OK) {
Bitmap bm = BitmapFactory.decodeFile(mFile.getAbsolutePath());
}
// do whatever you need with the Bitmap
Keep in mind that mFile will have to be global or somehow persisted so that it may be called upon where necessary.