How to get image from camera

后端 未结 1 906
青春惊慌失措
青春惊慌失措 2021-01-29 10:05

I have tried many codes from stackoverflow and internet.But I am able to find how to pick image from camera.I have used the following code,but data.getData() always returns null

相关标签:
1条回答
  • 2021-01-29 10:45

    Try this code @Thrishool,

    private static final int CAMERA = 1;
    
    //choosing the image from camera
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA);
    
    
    
    //now get the data from the onActivity result
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
       if (requestCode == CAMERA) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            imageview.setImageBitmap(thumbnail);
            ByteArrayOutputStream baos=new  ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
            byte [] b=baos.toByteArray();
            String temp = Base64.encodeToString(b, Base64.DEFAULT);
            Log.e("savedImage",temp);
    
            Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
        }
    }
    

    Try this and let me know @Thrishool

    0 讨论(0)
提交回复
热议问题