How to take a photo, save it and get the photo in Android

后端 未结 4 2104
情书的邮戳
情书的邮戳 2021-02-06 02:33

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 w

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 03:10

    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:

    
    
    

    for more informations a suggest: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView https://www.youtube.com/watch?v=je9bdkdNQqg

提交回复
热议问题