1

In my application, camera image save and retrieve from SQLite. I have camera uri from phonegap api but i want to convert camera uri to blob object and base64 string. I found some of the solution from online but i can't solve my problem. This is convert Base 64 to Blob object. I already tried that link. I can't convert to blob object.

using phonegap-1.4.1.js

Index.js

function onPhotoFileSuccess(imageData){
    console.log(JSON.stringify(imageData));

    var smallImage = document.getElementById('imageview'+imageviewcount);
    smallImage.style.display = 'block';
    smallImage.src = imageData;

    // "imageData" parameter is image uri
    // I don't know how to convert image uri to blob object

    // save to SQLite
    var db = window.sqlitePlugin.openDatabase(Mydb , ver, "", size);        

    db.transaction(function(tx) {tx.executeSql("Insert Into "+ DOPhotoTable +"( RefTranNo,Photo) values(?,?);",["007", blobobject]);});
}

function takeCameraImage(){
    navigator.camera.getPicture(onPhotoFileSuccess, onFail, {  quality: 30, destinationType: Camera.DestinationType.FILE_URI });
}

In onPhotoFileSucces function, i want to convert image uri to blob object.

Community
  • 1
  • 1
B M
  • 1,863
  • 1
  • 25
  • 40

1 Answers1

0

I tried canvas to blob api and base 64 string to blob code. but not ok for me. So that i maked some of the trick. I don't convert camera uri to blob in javascript. That process make in android native. Android native can easy to convert camera uri to blob. I called to android native code from javascript.

Index.js (Phonegap)

function onPhotoFileSuccess(imageData){
    var smallImage = document.getElementById('imageview'+imageviewcount);
    smallImage.style.display = 'block';
    smallImage.src = imageData;

    window.PhonegapActivity.insertPhoto(reftranNo,imageData);
}

function btn_Camera_click(){
   navigator.camera.getPicture(onPhotoFileSuccess, onFail, {  quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

PhonegapActivity (Android Native)

@Override
public boolean onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     super.setIntegerProperty("loadUrlTimeoutValue", 60000);
     //fix browser cache settings
     if (super.appView != null) {
        super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
     }

     super.loadUrl("file:///android_asset/www/index.html");

     // *** for send data from javascript
     super.appView.addJavascriptInterface(this, "PhonegapActivity");
}
public String insertPhoto(String reftranNo, String photo){
    try {

        Uri photoUri = Uri.parse(photo);
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),photoUri);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 70, stream);
        byte[] blob = stream.toByteArray();

        dbProvider _db = new dbProvider(this);
        _db = _db.openToWrite();            
        _db.DOPhotoInsert(reftranNo, blob);
        _db.close();

        return true;

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("Insert DO Photo", e.toString());
        return false;
    }
}
Community
  • 1
  • 1
B M
  • 1,863
  • 1
  • 25
  • 40