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 (attached image as evidence ) .
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
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.
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); }