0

I need to write an Android application which is capable of loading a Website via a WebView. The Website contains an Input (Type=FILE)

<form action="...">
    <input type="file" name="myFile" value="" id="..."><
    <input type="submit" value="submit"><
</form>

When loading is done, the application should use a specific Image and upload it via a storage path:

String Path = "/storage/emulated/0/myimage.jpg"

I already tried to open a FileChooser-Dialog and that works, but I need a solution without the filechooser. The path of the "Path"-Variable should be used. I know that this would be a huge security leak, but is there maybe a solution for this? Maybe change die input-value via JavaScript? Maybe with a Library?

PS: Not meant to do anything illegal - the company app is generating profile images and they need to be uploaded via an existing unchangeable Input of type File.

Thanks in advance.

2 Answers 2

2

You can try this:

1) Get the URI of your file.


2) Trigger file chooser


3) Override onShowFileChooser inside WebChromeClient like this:

ValueCallback<Uri[]> mFilePathCallback;
@Override
 public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

              if (mFilePathCallback != null) {
                    mFilePathCallback.onReceiveValue(null);
                }
                mFilePathCallback = filePathCallback;
                mFilePathCallback.onReceiveValue(myURI);
                mFilePathCallback = null;

                return true;

}

Here myURI is the URI of your file.

Sign up to request clarification or add additional context in comments.

1 Comment

Make sure you have storage permission before trying this.
0

Check my answer here. What you basically need to do is override WebChromeClient and its method openFileChooser.

class MyWebChromeClient extends WebChromeClient {
    // For 3.0+ Devices (Start)
    // onActivityResult attached before constructor
    protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }


    // For Lollipop 5.0+ Devices
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(null);
            uploadMessage = null;
        }

        uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(intent, REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            uploadMessage = null;
            Toast.makeText(WebLink.this, "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    //For Android 4.1 only
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        mUploadMessage = uploadMsg;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
    }

    protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }
}

2 Comments

Thanks for your answer, but that opens the FileChooser, am I right? - I need a solution to upload a file from a specific path, without opening a chooser.
@ManuelMaurer Yes, you're right. But you can customize it. and return the result in the same method using uploadMessage.onReceiveValue method without the intent to select file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.