How to save camera image in a new android image file?

This thread was migrated from an old forum. It may contain information that are no longer valid. For further assistance, please post a new question or open a support ticket from the Customer Support portal.

Hi guys, I am struggle with this problem. I would like to save my camera image in a file. This is my code

function onCameraEvent(){
frmMain.imgCamera.rawBytes = frmMain.camera.rawBytes;  // THIS LINE IS TOTALLY OK!! IT DISPLAYS THE PHOTO TAKEN IN AN IMAGE WIDGET 

   // HERE STARTS THE PROBLEM :(

var myfile = kony.io.FileSystem.getFile("/sdcard/duplog/");
var bResult = myfile.createDirectory(); 
alert("Directory creation ---"+bResult);
myfile = kony.io.FileSystem.getFile("/sdcard/duplog/cocoroco.jpg");
bResult = myfile.createFile(); 
alert("File creation ---"+bResult);
var dataAdded = myfile.write(frmMain.camera.rawBytes,true);

}

However, when I see my new created file, It is not ok :cry: (attached image as evidence ) .

Help!!! :cry::cry::cry::cry::cry:

Hi,

In Android, camera.base64 returns null value and the "camera.rawBytes" returns the path "content://...

Please try with the below code

function onCameraEvent(){

try{

frmMain.imgCamera.rawBytes = frmMain.camera.rawBytes;

var rimage = kony.image.createImage(frmMain.camera.rawBytes);

var base64FromImage = kony.convertToBase64(rimage.getImageAsRawBytes());

var path = kony.io.FileSystem.getDataDirectoryPath();

var myfile = kony.io.FileSystem.getFile(path + "/photos/lMediaName.jpg");

myfile.createFile();

var imgData = base64FromImage;

var dataAdded = myfile.write(imgData,false);

var photoDirectory = kony.io.FileSystem.getFile(path+"/photos/");

var fileList = photoDirectory.getFilesList();

var getfile = kony.io.FileSystem.getFile(fileList.item(0).fullPath);

getfile.createFile();

frmMain.imgCamera.rawBytes = getfile.read();

}

Hello dude, sorry for the delayed response. I try this two lines

frmMain.imgCamera.rawBytes = frmMain.camera.rawBytes; var rimage = kony.image.createImage(frmMain.camera.rawBytes);

However, I get the message "Cannot call method 'createImage' of undefined. I'm still trying to make it work in kony, I don't want to use FFI 😭

**Image removed for security reasons**

Hi,

Can you try the below,

  1. frmMain.imgCamera.rawBytes = frmMain.camera.rawBytes;
  2. var rimage = kony.image.createImage(frmMain.imgCamera.rawBytes);

Also confirm your Kony version details, to check the version supports the Image Object API's or not.

Same message 😞

My kony version is 6.5 **Image removed for security reasons**

Hi,

I would like to add this information. I replaced the rawbytes from the camera image for httprequest rawbytes

var request = new kony.net.HttpRequest(); request.onReadyStateChange=realizeCamera; request.open(constants.HTTP_METHOD_GET, "http://docs.kony.com/5_6/konyonpremises/Skins/Default/Stylesheets/Images/konylogo.png"); request.send(); function realizeCamera(){ var myfile = kony.io.FileSystem.getFile("/sdcard/dupont/"); var bResult = myfile.createDirectory(); myfile = kony.io.FileSystem.getFile("/sdcard/dupont/img.jpg"); bResult = myfile.createFile(); var dataAdded = myfile.write(this.response,true); }

It works correctly, but I wanna save the camera image instead 😭 . Maybe is my kony version??? My android device version is 7.0

Hello,

Thank you for confirming the Kony version. Kony-6.5 version doesn't have image object API support and when we trying to access the camera rawBytes or image rawBytes (from the camera) it always returns the address of the image instead of raw bytes.

Now the current functionality saving the address of the image as jpg and the saved image is corrupted. Kony has given the Image Object API and Save Image Gallery API in Kony-7.x version.

Hi,

Hope we have answered your post. If so, please click [Select as Best] below his response to close this post out. Thanks!

Hi!!!

Sorry for the delay, I resolved my question using Kony (6.5) and Android Native . So, I'm sharing the relevant information from my source code.

Android Native

public static int openGallery(Function onResultGalleryIMG) { fun = onResultGalleryIMG; KonyMain.getActivityContext().registerActivityResultListener(1, new ResultListener()); Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); KonyMain.getActivityContext().startActivityForResult(intent, 1); return 0; } static class ResultListener implements ActivityResultListener { public void onActivityResult(int requestCode, int resultCode, Intent data) { final Uri imageUri = data.getData(); final InputStream imageStream; String[] dataToSend = new String[2]; try { imageStream = KonyMain.getActivityContext().getContentResolver().openInputStream(imageUri); // imageStream = KonyMain.getActivityContext()getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); Bitmap resizedIMG = Bitmap.createScaledBitmap(selectedImage, 120, 120, true); String encodedImage = encodeImage(selectedImage); String encodedImageResized = encodeImage(resizedIMG); dataToSend[0] = encodedImage; int valor = saveIMG(encodedImage,"ImagenPrueba001"); //String imgCompressed = comprimirImagenNuevo("ImagenPrueba001"); int valor2 = saveIMG(encodedImageResized,"ImagenPruebaCmpr001"); fun.executeAsync(dataToSend); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } private static String encodeImage(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); return encImage; } public static int saveIMG(String base64String, String name) { try { String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); ByteArrayOutputStream f = new ByteArrayOutputStream(); byte[] buf = Base64.decode(base64String, 0); f.write(buf); OutputStream f2 = new FileOutputStream( "/sdcard/proyectoAPP"+ "/" + name + ".jpg"); f.writeTo(f2); f2.close(); return 1; } catch (Exception e) { System.out.println(e); } return 0; }

Kony Studio

function onResultGalleryIMG(result){ frmMain.imgCamera.base64 = result; } function openSaveGalleryFFI(){ var myfile = kony.io.FileSystem.getFile("/sdcard/proyectoAPP/"); var bResult = myfile.createDirectory(); var myfile = kony.io.FileSystem.getFile("/sdcard/proyectoAPP/"); bResult = myfile.createFile(); var valueReturned = GerardoPlugin.openGallery(onResultGalleryIMG); }

I'm going to selected my answer as Best :D

Thanks for sharing your solution with the Community!