我的应用程序是基于网络的,需要上传照片,网站有一个文件输入按钮,我用这个工作了
wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );
}
但它只显示了选择照片的画廊,我需要同时从相机拍摄。
我尝试了这个解决方案Upload camera photo and filechooser from webview INPUT field但是它唯一的开放式相机,而不是上传拍摄的照片
答案 0 :(得分:0)
在你的例子中
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView v, String url) {
if (url.startsWith("testzapp:")) {
//do whatever action you had intended
}
}
}
答案 1 :(得分:0)
这是我的解决方案
if (url.startsWith("xxx")) {
String[] falid = url.split(":");
falidi = Integer.parseInt(falid[1]);
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
return true;
}
public void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PICTURE) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Random randomGenerator = new Random();
randomGenerator.nextInt();
newimagename = falidi + "_" + randomGenerator.nextInt()
+ ".jpg";
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + newimagename);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f.getAbsoluteFile());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String uri = f.getAbsolutePath();
// this is the url that where you are saved the
// image
File fx = new File(uri);
Bitmap bitmap = BitmapFactory.decodeFile(fx.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr,
Base64.DEFAULT);
client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://www.xxx.com/android/library/image.php?name="
+ newimagename);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("image", image_str));
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Candemir
new ResultTask().execute(new HttpPost[] { post });
// Candemir
} catch (Exception e) {
e.printStackTrace();
}
}
}
}