Created
October 6, 2011 17:43
-
-
Save andy722/1268074 to your computer and use it in GitHub Desktop.
WebView: file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this for all extension of files, such as pdf, doc, png, ppt ?