Home > Software engineering >  How to convert a camera image byte array to Uri in android studio
How to convert a camera image byte array to Uri in android studio

Time:10-24

I am making a custom camera and I want to capture the image and convert the byte array result into an uri for uploading on firebase Storage. I tried this but it gives an fileNotFoundExeption

Camera.PictureCallback mPictureCallback = (data, camera) -> {
    File pictureFile = getOutputMediaFile();
    if (pictureFile == null){
        return;
    }else {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(pictureFile);
            fileOutputStream.write(data);
            fileOutputStream.close();
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};

@RequiresApi(api = Build.VERSION_CODES.R)
private File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)){
        return null;
    }else {
        @SuppressLint("SdCardPath") File folder_gui = new File(Environment.getExternalStorageDirectory()   "/Captured Images");
        if (!folder_gui.exists()){
            folder_gui.mkdirs();
        }
        Random random = new Random();
        String name = "Image - "   random.nextInt(1234567)   "-"   random.nextInt(1234567);
        File outputFile = new File(folder_gui, name);
        return outputFile;
    }
}

private void captureImage(){
    if (camera != null){
        try {
            camera.takePicture(null, null, mPictureCallback);
        }catch (RuntimeException e){
            e.printStackTrace();
        }
    }
}

For converting I tried this in the mPictureCallback Method

String str = new String(data,"UTF-8");
Uri fileUri = Uri.parse(str);
Log.d("fileDataUri",fileUri.toString()   "");

But I get some some random characters.

Now to convert it what do I need to do because I really need to implement it.

Thanks

  • Related