AndroidStudioで開発しています。
WebViewにPOSTで画像ファイルデータを渡す方法を教えてください。
POSTしたいデータは、2つです。
uploadImageType ← "1" または "2"
uploadImage ← JPEGなどの画像ファイル
webView.postUrl の第二パラメータに、POSTしたいString型のデータをbite[]に変換して渡していますが、
受け取り側ではファイル型式が正しくないと認識されてしまいます。
おそらく、uploadImage の渡し方が正しくないのだと思います。
String reductionPath = "file:///storage/emulated/0/Pictures/hoge/photo.jpg";
String data = "uploadImageType=1&uploadImage=";
try {
data += readFileToString(reductionPath);
} catch (Exception e) {
int x = 0;
}
webView.postUrl(url, data.getBytes());
private String readFileToString(String filePath) throws Exception {
filePath = filePath.replace("file://", "");
byte[] b = new byte[1];
FileInputStream fis = new FileInputStream(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (fis.read(b) > 0) {
baos.write(b);
}
baos.close();
fis.close();
String str = baos.toString();
return str;
}
webView.postUrl(url, data.getBytes()); を
webView.postUrl(url, EncodingUtils.getBytes(data, "BASE64")); に変えてもダメでした。
※本来はHTMLで <input type="file" ...> とするのですが、
Android4.4ではそれが正しく機能しないようなので、上記の手法を試みています。
よろしくお願いします。