Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Click a button to take a photo.
- 2. Save photo.
- 3. Add photo into the gallery.
- 4. Show photo into an ImageView.
- photoFile = createImageFile();
- private File createImageFile() throws IOException {
- // Create an image file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imageFileName = timeStamp;
- File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg");
- if (imageFile.exists()){
- imageFile.delete();
- imageFile.createNewFile();
- }
- else{
- imageFile.createNewFile();
- }
- mCurrentPhotoPath = imageFile.getAbsolutePath();
- return imageFile;
- }
- if (photoFile != null) {
- Uri photoURI = FileProvider.getUriForFile(getContext(),
- "es.antonio.prueba.fileprovider",
- photoFile);
- takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
- startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
- }
- private void galleryAddPic() {
- Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
- File f = new File(mCurrentPhotoPath);
- Uri contentUri = Uri.fromFile(f);
- mediaScanIntent.setData(contentUri);
- getContext().sendBroadcast(mediaScanIntent);
- }
- private void setPic(ImageView mImageView) {
- BitmapFactory.Options bmOptions = new BitmapFactory.Options();
- // Decode the image file into a Bitmap sized to fill the View
- bmOptions.inJustDecodeBounds = false;
- bmOptions.inPurgeable = true;
- Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
- mImageView.setImageBitmap(bitmap);
- }
Add Comment
Please, Sign In to add comment