[英]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.