Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazem Hagrass committed Nov 6, 2013
1 parent ad11d83 commit 15e5419
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/android/Base64Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

package com.badrit.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.json.JSONArray;
Expand Down Expand Up @@ -52,17 +54,24 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo

private String encodeFile(String filePath) {
String imgStr = "";
filePath = filePath.replaceAll("file://", "");
File imageFile = new File(filePath);
if(!imageFile.exists())
try {
filePath = filePath.replaceAll("file://", "");
File imageFile = new File(filePath);
if(!imageFile.exists())
return imgStr;

byte[] bytes = new byte[(int) imageFile.length()];

FileInputStream fileInputStream = new FileInputStream(imageFile);
fileInputStream.read(bytes);

imgStr = Base64.encodeToString(bytes, Base64.DEFAULT);
imgStr = "data:image/*;charset=utf-8;base64," + imgStr;
} catch (Exception e) {
return imgStr;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
imgStr = Base64.encodeToString(image, 0);
imgStr = "data:image/png;charset=utf-8;base64," + imgStr;
}
return imgStr;

}


Expand Down

0 comments on commit 15e5419

Please sign in to comment.