Skip to content

Instantly share code, notes, and snippets.

@andy722
Created October 6, 2011 17:43
Show Gist options
  • Save andy722/1268074 to your computer and use it in GitHub Desktop.
Save andy722/1268074 to your computer and use it in GitHub Desktop.
WebView: file upload
/**
* New user registration.
*/
public class RegistrationFormActivity extends Activity {
// for file uploading
private final static int FILE_CHOOSER_RESULT_CODE = 1;
private ValueCallback<Uri> uploadFile;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv = new WebView(this);
wv.setWebChromeClient(new RegistrationWebChromeClient());
// ...
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (uploadFile == null)
return;
final Uri result = ((intent == null) || (resultCode != RESULT_OK)) ? null : intent.getData();
uploadFile.onReceiveValue(result);
uploadFile = null;
}
}
private class RegistrationWebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.w(sourceID, message);
}
//@Override
// Dirty hack here: overriding hidden method to handle <input type='file'/>
@SuppressWarnings({"UnusedDeclaration"})
public void openFileChooser(ValueCallback<Uri> uploadMessage) {
uploadFile = uploadMessage;
final Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILE_CHOOSER_RESULT_CODE);
}
}
// ...
}
@tanim3221
Copy link

Is this for all extension of files, such as pdf, doc, png, ppt ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment