簡體   English   中英

在onActivityResult()之后調用的Android openFileChooser onCreate()

[英]Android openFileChooser onCreate() called after onActivityResult()

我有一個webview,在其中一個頁面上有一個Upload Photo按鈕。 我找到了一些實現文件選擇器的代碼(Android,為什么這么難???),如果我選擇圖庫,一切正常。 如果我選擇10倍的相機1,它將起作用。 但是大多數情況下,當我拍照並單擊“保存”(這全部在相機活動中)時,Web視圖會加載啟動應用程序時加載的第一頁。 似乎沒有調用onActivityResult() ,而是調用了onCreate() ,這使我的應用程序混亂。 能給我一個例子,說明我拍照后如何還原webView狀態嗎? (也許我應該提到我已經在WebView中登錄了)。

這是WebChromeClient類:

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");
}

運行文件選擇器時,系統可能正在暫停/停止您的活動。

您需要處理onResume()並還原活動的先前狀態。 當您的活動重新啟動時,將調用onResume方法,因此這是在啟動文件選擇器之前重新加載所顯示的任何WebView的好地方。

通過在清單android:configChanges="keyboardHidden|orientation|screenSize"添加此行來android:configChanges="keyboardHidden|orientation|screenSize"

查看您發布的代碼,這是一條危險的線:

webView.requestFocus();

首先在onActivityResult() 如果正在重新創建MainActivity ,則可能導致NullPointerException 系統將無縫地重新啟動您的應用程序,可能會產生報告的行為。

onActivityResult()的開頭添加另一個Log.d(),在該危險行周圍進行嘗試/捕獲, 使用onSaveInstanceState()和onRestoreInstanceState()。

請注意,如果您的MainActivity是Portrait,則您會面臨額外的麻煩,因為Camera應用會強制使用Landscape。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM