0

我有一個web視圖,並在其中一個頁面上有一個上傳照片按鈕。我發現一些代碼來實現文件選擇器(Android,爲什麼這麼難?),如果我選擇一切正常工作。如果我選擇10倍的相機1的作品。但大多數情況下,當我拍攝照片並單擊保存時(這全部在相機活動中),webview加載啓動應用程序時加載的第一個頁面。似乎onActivityResult()不叫,但它而不是onCreate()是,這弄亂了我的應用程序。你能給我一個如何在拍攝照片後恢復webView狀態的例子嗎? (也許我應該提到我在WebView中登錄)。Android openFileChooser onCreate()onActivityResult()後調用

這是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"); 
} 
+0

加入這一行_ [這裏](http://stackoverflow.com/questions/20424909/android-startcamera-gives-me-null-intent-and-does-it固定-destroy-my-global-varia)_描述了類似的情況,並提供瞭解決方案。 –

+0

我不明白如何保存webview狀態並將其恢復。如果我在onRestoreInstanceState()中恢復它,頁面不會從相機拍攝照片。您是否會舉例說明? – definera

+0

等一下。爲什麼'onRestoreInstanceState()'與相機衝突?或者,你沒有看到在這種情況下調用的'onActivityResult()'? –

回答

1

通過在清單android:configChanges="keyboardHidden|orientation|screenSize"

1

你的活動可能被暫停/由系統停止,而你運行該文件選擇器。

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

+0

10我應該在onResume中做什麼? – definera

+0

請參閱我編輯的答案。 –

0

看您發佈的代碼,這裏是一個危險的線:

webView.requestFocus(); 

是首次在onActivityResult()。如果MainActivity正在重新創建,這可能會導致空指針異常。系統將無縫地重新啓動您的應用程序,可能會產生報告的行爲。

另一個Log.d()加入的onActivityResult()開始,把try/catch語句解決這個危險的線路,的onSaveInstanceState使用()和onRestoreInstanceState()。

請注意,如果您的MainActivity是肖像,您將面臨額外的麻煩,相機應用強制橫向。

相關問題