I want to upload a file in a webview. I'm able to upload the files in devices till Android 4.3 & above Android 4.4.3. using openFileChoser() & showFileChoser(for lollypop). But openFileChoser() doesn't get called in Android 4.4.1 & Android 4.4.2. So I implemented a custom file choser dialog. And I'm able to select the file. But problem is, I'm not able to inform the webview that user has selected the file. So that it informs the tag that file is chosen and show the file name in the webview. I tried by calling javascript of the html page through code,. But nothing happens and in the console I can see this message:
"Uncaught SyntaxError: Unexpected identifier", in the html file.
Here is my onActivity Result code, where I'm calling javascript of the html page:
/****Activity Class****/
public class MainActivity extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
super.onActivityResult(requestCode, resultCode, intentData);
if (kitkat()) {
String realPathFromURI = "";
String fullPathUri = null;
if (intentData != null) {
Uri uri = intentData.getData();
if (uri != null) {
Uri uri1 = Uri.parse(uri.toString());
realPathFromURI = Utility.getRealPathFromURI(this, uri1);
if (null != realPathFromURI) {
File file = new File(Utility.getRealPathFromURI(this, uri1));
boolean isMaxSize = Utility.isMaxFileSize(file.length());
if (isMaxSize) {
if (this != null)
Toast.makeText(this.getApplicationContext(), "File Size too large",
Toast.LENGTH_SHORT).show();
return;
}
fullPathUri = file.toString();
} else {
if (this != null)
Toast.makeText(this.getApplicationContext(), "Invalid File Format", Toast.LENGTH_SHORT)
.show();
}
}
} else {
realPathFromURI = "";
fullPathUri = mCameraPhotoPath;
}
Bitmap bm = null;
;
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
bm = BitmapFactory.decodeFile(fullPathUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
ImagePath oPath = new ImagePath(fullPathUri, encodedImage);
String sJSON = new com.google.gson.Gson().toJson(oPath);
String msgToSend = realPathFromURI;
// browser.loadUrl("javascript:KitKatWebviewChooseImgResult(\"" + msgToSend + "\")");
browser.loadUrl("javascript:ShowImageResult(\"" + sJSON + "\")");
}
else{
// code for all versions
}
}
/fileupload.html/
<form>
<input type="file" name="file" id="files" style="width:195px;font-size:12px;" onclick="showFileChoser()" />
<p id="path">No File chosen</p>
<input type="button" id="mySubmit" value="submit" style="height:100px;width:200px;" onclick="this.disabled = true;form1.submit();" disabled="true" />
////
///
</form>
<script>
function KitKatWebviewChooseImgResult(msg) {
document.getElementById("path").innerHTML = msg;
}
function showFileChoser(){
choser.choseImage();
}
function ShowImageResult(obj){
var image= document.getElementById("files");
image.value=obj.Base64;
}
</script>
Is it a right method to convert a class to json object. " String sJSON = new com.google.gson.Gson().toJson(oPath);" I'm I doing anything wrong.. . Thanks Advance..
Aucun commentaire:
Enregistrer un commentaire