Android openFileChooser onCreate () is called after onActivityResult ()

I have a web view, and on one of the pages there is a Upload photo button. I found code for implementing file selection (Android, why is it so complicated ????), and if I select a gallery, everything works fine. If I select the camera 1 out of 10 times, it will work. But most of the time, when I take a picture and click “Save” (this is all in the camera’s activity), the webview loads the first page loaded when the application starts. It onActivityResult()does not seem to be called, but instead it onCreate()is, and this will ruin my application. Can you give me an example of how to restore the state of a webView after I take a picture? (maybe I should mention that I logged in to WebView).

This is the WebChromeClient class:

public class WebViewChromeClient extends WebChromeClient {
    private Activity activity;
    public Uri imageUri;

    private static final int FILECHOOSER_RESULTCODE = 1;
    private Uri mCapturedImageURI = null;

    private Context context;

    private MainActivity mainActivity;

    public WebViewChromeClient(Context context, Activity activity,
            MainActivity mainActivity) {
        this.activity = activity;
        this.context = context;
        this.mainActivity = mainActivity;
    }

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {

        // Update message
        ((Audi) activity.getApplication()).setmUploadMessage(uploadMsg);

        if (uploadMsg == null) {
            Log.d("UPLOAD MESSAGE", "NULL");
        }

        try {
            File imageStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "AndroidExampleFolder");

            if (!imageStorageDir.exists()) {
                // Create AndroidExampleFolder at sdcard
                imageStorageDir.mkdirs();
            }

            // Create camera captured image file path and name
            File file = new File(imageStorageDir + File.separator + "IMG_"
                    + String.valueOf(System.currentTimeMillis()) + ".jpg");

            mCapturedImageURI = Uri.fromFile(file);
            mainActivity.setmCapturedImageURI(mCapturedImageURI);
            Log.d("Line", "57");
            // Camera capture image intent
            final Intent captureIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            mainActivity.setmCapturedImageURI(mCapturedImageURI);

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");

            // Create file chooser intent
            Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

            // Set camera intent to file chooser
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    new Parcelable[] { captureIntent });

            // On select image call onActivityResult method of activity
            activity.startActivityForResult(chooserIntent,
                    FILECHOOSER_RESULTCODE);

        } catch (Exception e) {
            Toast.makeText(context, "Exception:" + e, Toast.LENGTH_LONG).show();
        }

    }

    // openFileChooser for Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser(uploadMsg, "");
    }

    // openFileChooser for other Android versions
    public void openFileChooser(ValueCallback<Uri> uploadMsg,
            String acceptType, String capture) {

        openFileChooser(uploadMsg, acceptType);
    }

    // The webPage has 2 filechoosers and will send a
    // console message informing what action to perform,android wml_siso init 
    // taking a photo or updating the file

    public boolean onConsoleMessage(ConsoleMessage cm) {

        onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
        return true;
    }

    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
         Log.d("androidruntime", "Show console messages, Used for debugging: "
         + message);

    }

}

onActivityResult:

@Override 
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {

    webView.requestFocus();

    if (requestCode == FILECHOOSER_RESULTCODE) {
        Log.d("MainActivity", "onActivityResult");

        if (null == ((Audi) getApplication()).getmUploadMessage()) {
            Log.d("FileChooser Result", "58");
            return;
        }

        Log.d("MainActivity", "onActivityResult");
        Uri result = null;

        try {
            if (resultCode != RESULT_OK) {
                result = null;
            } else {
                // retrieve from the private variable if the intent is null
                result = intent == null ? mCapturedImageURI : intent
                        .getData();
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "activity :" + e,
                    Toast.LENGTH_LONG).show();
        }

        ((Audi) getApplication()).getmUploadMessage().onReceiveValue(result);
        ((Audi) getApplication()).setmUploadMessage(null);
    }
    Log.d("MainActivity", "onActivityResult");
}
0
3

android:configChanges="keyboardHidden|orientation|screenSize"

+1

, , / .

onResume() . onResume , , WebView, , .

+1

, :

webView.requestFocus();

onActivityResult(). MainActivity , NullPointerException. , , .

Log.d() onActivityResult(), try/catch onSaveInstanceState() onRestoreInstanceState().

, MainActivity - , , "" .

0

Source: https://habr.com/ru/post/1620728/


All Articles