问题
My application is webbased and need to upload photos, website have a file input button, i made it work with this
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 );
}
but it shows just gallery to pick photos, i need to take from camera at the same time.
i tried this solution Upload camera photo and filechooser from webview INPUT field but its only opening camera, not uploading taken photo
回答1:
In your example
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView v, String url) {
if (url.startsWith("testzapp:")) {
//do whatever action you had intended
}
}
}
回答2:
here is my solution
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();
}
}
}
}
来源:https://stackoverflow.com/questions/17968787/upload-photo-from-gallery-or-take-from-camera-in-webview