简体   繁体   English

如何在android studio中将相机图像字节数组转换为Uri

[英]How to convert a camera image byte array to Uri in android studio

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.我正在制作一个自定义相机,我想捕获图像并将字节数组结果转换为 uri 以上传到 firebase Storage。 I tried this but it gives an fileNotFoundExeption我试过了,但它给出了一个 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为了转换,我在 mPictureCallback 方法中尝试了这个

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 🙂谢谢🙂

您可以使用:

Uri.fromFile(pictureFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将图像转换为字节数组 - how to convert an image to byte array in android Xamarin android映像URI到Byte数组 - Xamarin android image URI to Byte array 如何在 Android Studio 中将图像视图转换为字节数组? - How Can I convert an imageview to byte array in Android Studio? 如何将图像转换为字节数组并将后字节数组转换为图像 - How to convert image to byte array and convert back byte array to image 如何获取Android相机中takePicture调用返回的图像数据字节数组? - How to get image data byte array returned from takePicture call in Android camera? 用字节数组更改后如何将图像字节数组转换为位图 - How to convert image byte array into a bitmap after altering with the byte array 将字节数组转换为Image - Convert byte array to Image 如何通过Soap Web服务(XML)传递图像的字节数组并在Android设备上对该字节数组进行解码以将其转换回图像 - How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image 如何将字节数组RGB图像转换为灰度 - How convert byte array RGB image into greyscale 如何将字节数组转换为缓冲图像 - How to convert byte array to buffered image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM