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

后端 未结 4 2113
情书的邮戳
情书的邮戳 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条回答
  •  梦谈多话
    2021-02-06 03:13

    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);
               } 
             }
    
    
        }
    

提交回复
热议问题