How to take a photo, save it and get the photo in Android
Solution 1
define a variable like this
protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
Use the code for calling camera from android.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
and in the class calling this override the onActivityResult function and enter the code below.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//use imageUri here to access the image
Bundle extras = data.getExtras();
Log.e("URI",imageUri.toString());
Bitmap bmp = (Bitmap) extras.get("data");
// here you will get the image as bitmap
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
}
Solution 2
there are several example to capture image and store it and open it...
1. Android Camera API - Tutorial
Solution 3
I had the same problem.
I was testing some codes from the internet, and couldn't find any. Then, I studied some basics codes from developer.android. After that, I mixed two different codes and my one worked! Here it goes!
public class MainActivity extends AppCompatActivity {
static final int PICTURE_RESULT = 1;
String mCurrentPhotoPath;
ContentValues values;
private Uri file;
ImageView imageView;
Bitmap help1;
ThumbnailUtils thumbnail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
values = new ContentValues();
}
public void launch_camera(View v) {
// the intent is my camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//getting uri of the file
file = Uri.fromFile(getFile());
//Setting the file Uri to my photo
intent.putExtra(MediaStore.EXTRA_OUTPUT,file);
if(intent.resolveActivity(getPackageManager())!=null)
{
startActivityForResult(intent, PICTURE_RESULT);
}
}
//this method will create and return the path to the image file
private File getFile() {
File folder = Environment.getExternalStoragePublicDirectory("/From_camera/imagens");// the file path
//if it doesn't exist the folder will be created
if(!folder.exists())
{folder.mkdir();}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_"+ timeStamp + "_";
File image_file = null;
try {
image_file = File.createTempFile(imageFileName,".jpg",folder);
} catch (IOException e) {
e.printStackTrace();
}
mCurrentPhotoPath = image_file.getAbsolutePath();
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICTURE_RESULT) {
if(resultCode == Activity.RESULT_OK) {
try {
help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),file);
imageView.setImageBitmap( thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight()));
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
the XML files just have a Button and a ImageView and do not forget to declare in your android manifest the permissions:
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
for more informations a suggest: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView https://www.youtube.com/watch?v=je9bdkdNQqg
user1802057
Updated on March 06, 2020Comments
-
user1802057 about 4 years
I've been searching the simple example to take a photo, and save it using URI and retrieve the photo for image processing , I've tried lot of example code, but none of them went smoothly.
Is there anyone have the example code?
-
paulkayuk over 11 yearsDo you realise, that with the code you have supplied, you are saving the image full sized to the stated uri, BUT, you are retrieving the reduced sized image from the extras, and creating the bitmap from that reduced size image?
-
Pramod J George over 11 yearsI am retrieving the image from the uri as full sized. do you want reduced image
-
onexf about 7 years@paulkayuk how do i retrieve a full sized image image instead of a compressed one?
-
JPhi Denis over 6 yearsBitmap bmp = (Bitmap) extras.get("data") is the way to get the thumbnail
-
Daniel Dees over 5 yearsDoes anyone know how to get the full-sized image? I would really like to know. I've been searching through stackoverflow threads for a while now and only coming up on these 7 year old threads of people having the same issue with no response...