airsdk / Adobe-Runtime-Support

Report, track and discuss issues in Adobe AIR. Monitored by Adobe - and HARMAN - and maintained by the AIR community.
199 stars 11 forks source link

Input Flle inside Stagewebview/Android #3371

Closed marcanw closed 1 month ago

marcanw commented 1 month ago

My app uses a lot of StageWebView.

We have to use the following HTML code inside StageWebView in order to access the gallery: <input style="display:none" class=files type="file" accept="image/*,video/*" multiple onchange="resizeAndSaveImage(this.files);this.value=''">

It works fine on a normal web page and on iOS/StageWebView, but it doesn't work on Android/StageWebView.

After some research, I found that a WebView on Android needs a few lines of code to accept the file input.

Do you think it's possible to add this feature to unlock the file input from StageWebView on Android? It is really important for us; otherwise, we might be forced to switch to another platform (which would make me very sad to quit Adobe Air).

Thank you for your help.

Here is the code I found:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ValueCallback<Uri[]> filePathCallback;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {

            // For Android 5.0+
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                MainActivity.this.filePathCallback = filePathCallback;
                Intent intent = fileChooserParams.createIntent();
                try {
                    startActivityForResult(intent, 1);
                } catch (android.content.ActivityNotFoundException e) {
                    filePathCallback.onReceiveValue(null);
                    return false;
                }
                return true;
            }
        });

        webView.loadUrl("https://example.com");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (filePathCallback != null) {
                Uri[] results = (resultCode == RESULT_OK && data != null) ? new Uri[]{data.getData()} : null;
                filePathCallback.onReceiveValue(results);
                filePathCallback = null;
            }
        }
    }
}
ajwfrost commented 1 month ago

That sort of change should be fairly straightforward. I'm wondering whether there's any downside in adding it just as a blanket update for AIR, or whether we should only have this capability added on-demand.

TBH seems a little odd that this capability isn't built into the WebChromeClient by default; but maybe they need this extra bit of glue just to conform with how intent/activity results are handled.

We'll see if we can squeeze this in for our 51.1 update...

thanks

marcanw commented 1 month ago

Great! Thank you.

marcanw commented 1 month ago

@ajwfrost Is there a release date for the 51.1 update? I need to inform my client. Thank you.

ajwfrost commented 1 month ago

It was meant to be early in the coming week .. aiming for the end of the week now as we have picked up a weird regression issue that we need to eliminate first!

marcanw commented 1 month ago

Thank you @ajwfrost it works like a charm with the last AIR.