溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android項(xiàng)目中如何在webview頁(yè)面中上傳文件

發(fā)布時(shí)間:2020-11-26 15:56:27 來(lái)源:億速云 閱讀:454 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

本篇文章為大家展示了Android項(xiàng)目中如何在webview頁(yè)面中上傳文件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

Android webview在默認(rèn)情況下是不支持網(wǎng)頁(yè)中的文件上傳功能的;

如果在網(wǎng)頁(yè)中有<input type="file" />,在android webview中訪問(wèn)時(shí)也會(huì)出現(xiàn)瀏覽文件的按鈕

但是點(diǎn)擊按鈕之后沒(méi)有反應(yīng)...

那么如何能夠讓android的webview能夠響應(yīng),這個(gè)瀏覽按鈕呢?在網(wǎng)上查了很多資料,很多相同的,但都漏掉了一個(gè)地方,導(dǎo)致無(wú)法讀取到文件的完整地址(“c:\upfile\233232.jpg”),整理最終代碼入下:

我們需要為webview設(shè)置WebChromeClient,在WebChromeClient的實(shí)現(xiàn)類中覆蓋文件選擇的方法:

package com.example.webviewupfile; 
 
import java.io.File; 
import java.io.IOException; 
 
import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.webkit.ValueCallback; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 
 
public class MainActivity extends Activity { 
  private ValueCallback<Uri> mUploadMessage; 
  private final static int FILECHOOSER_RESULTCODE = 1; 
  private WebView web; 
  private ProgressBar progressBar; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    web = (WebView) findViewById(R.id.webView1); 
    progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
 
    web = new WebView(this); 
    web.getSettings().setJavaScriptEnabled(true); 
    web.loadUrl("http://ueditor.baidu.com/website/onlinedemo.html"); 
    web.setWebViewClient(new myWebClient()); 
    web.setWebChromeClient(new WebChromeClient() { 
      // The undocumented magic method override 
      // Eclipse will swear at you if you try to put @Override here 
      // For Android 3.0+ 
      public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE); 
        i.setType("image/*"); 
        MainActivity.this.startActivityForResult( 
            Intent.createChooser(i, "File Chooser"), 
            FILECHOOSER_RESULTCODE); 
 
      } 
 
      // For Android 3.0+ 
      public void openFileChooser(ValueCallback uploadMsg, 
          String acceptType) { 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE); 
        i.setType("*/*"); 
        MainActivity.this.startActivityForResult( 
            Intent.createChooser(i, "File Browser"), 
            FILECHOOSER_RESULTCODE); 
      } 
 
      // For Android 4.1 
      public void openFileChooser(ValueCallback<Uri> uploadMsg, 
          String acceptType, String capture) { 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE); 
        i.setType("image/*"); 
        MainActivity.this.startActivityForResult( 
            Intent.createChooser(i, "File Chooser"), 
            MainActivity.FILECHOOSER_RESULTCODE); 
 
      } 
 
    }); 
 
    setContentView(web); 
  } 
 
  @Override 
  protected void onActivityResult(int requestCode, int resultCode, 
      Intent intent) { 
    if (requestCode == FILECHOOSER_RESULTCODE) { 
      if (null == mUploadMessage) 
        return; 
      Uri result = intent == null || resultCode != RESULT_OK ? null 
          : intent.getData(); 
       
      // mUploadMessage.onReceiveValue(result); 
      // mUploadMessage = null; 
      Bitmap bm = null; 
      //外界的程序訪問(wèn)ContentProvider所提供數(shù)據(jù) 可以通過(guò)ContentResolver接口 
      ContentResolver resolver = getContentResolver(); 
      try { 
        Uri originalUri = intent.getData(); // 獲得圖片的uri 
        bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); 
        // 這里開(kāi)始的第二部分,獲取圖片的路徑: 
        String[] proj = { MediaStore.Images.Media.DATA }; 
        // 好像是android多媒體數(shù)據(jù)庫(kù)的封裝接口,具體的看Android文檔 
        Cursor cursor = managedQuery(originalUri, proj, null, null, 
            null); 
        // 按我個(gè)人理解 這個(gè)是獲得用戶選擇的圖片的索引值 
        int column_index = cursor 
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
        // 將光標(biāo)移至開(kāi)頭 ,這個(gè)很重要,不小心很容易引起越界 
        cursor.moveToFirst(); 
        // 最后根據(jù)索引值獲取圖片路徑 
 
        String path = cursor.getString(column_index); 
        Uri uri = Uri.fromFile(new File(path)); 
        mUploadMessage.onReceiveValue(uri); 
      } catch (IOException e) { 
 
        Log.e("TAG-->Error", e.toString()); 
 
      } 
    } 
  } 
 
  public class myWebClient extends WebViewClient { 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
    } 
 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 
 
      view.loadUrl(url); 
      return true; 
 
    } 
 
    @Override 
    public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
 
      progressBar.setVisibility(View.GONE); 
    } 
  } 
 
  // flipscreen not loading again 
  @Override 
  public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
  } 
 
  // To handle "Back" key press event for WebView to go back to previous 
  // screen. 
  /* 
   * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if 
   * ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { web.goBack(); 
   * return true; } return super.onKeyDown(keyCode, event); } 
   */ 
}

上述內(nèi)容就是Android項(xiàng)目中如何在webview頁(yè)面中上傳文件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI