2017-02-12 79 views
1

我是android新手,現在嘗試開發web應用程序。我已經使用了幾天的時間來使輸入類型文件在webview中完美工作。直到我遵循並使用此代碼來處理webview中的文件選擇器。處理文件選擇器android 6.0 webview

enter link description here

當我使用AVD的Nexus與gingerbeard並單擊輸入類型的文件,它可以顯示兩個選項(相機或畫廊)。 該問題是當我使用AVD Nexus 6.0並點擊輸入類型文件時,它將打開文件管理器而不顯示選項相機或圖庫。 我想在每次用戶點擊輸入類型文件時顯示輸入選項對話框。

我該怎麼做?感謝您的幫助。

回答

2

爲Android 5.0及以上

private static final int INPUT_FILE_REQUEST_CODE = 1; 

private Context mContext=YourActivity.this; 
private static final int REQUEST_CAMERA = 111; 

// For Android 5.0 

public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) 
{ 
    // Log.d(TAG,"ShowFileChooser For Android 5.0 "); 
    if (mFilePathCallback != null) { 
     mFilePathCallback.onReceiveValue(null); 
    } 
    mFilePathCallback = filePath; 
    if (Build.VERSION.SDK_INT >= 23) { 
     // Log.d(TAG,"ShowFileChooser For Android 5.0 SDK_INT>=23 chk permission"); 
     String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}; 
     if (!hasPermissions(mContext, PERMISSIONS)) { 
      ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_CAMERA); 
     } else { 
      //Log.d(TAG,"ShowFileChooser For Android 5.0 in IF SDK_INT>=23 permission grant"); 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = Constant.create_file(); 
        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
       } catch (Exception ex) { 
        // Error occurred while creating the File 
        // Log.e(TAG, "Unable to create Image File", ex); 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
          Uri.fromFile(photoFile)); 
       } else { 
        takePictureIntent = null; 
       } 
      } 
      Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
      contentSelectionIntent.setType("image/*"); 
      Intent[] intentArray; 
      if (takePictureIntent != null) { 
       intentArray = new Intent[]{takePictureIntent}; 
      } else { 
       intentArray = new Intent[0]; 
      } 
      Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
      chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
      chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
      startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 
     } 
    } else { 
     // Log.d(TAG,"ShowFileChooser For Android 5.0 in else SDK_INT>=23"); 
     if (mFilePathCallback != null) { 
      mFilePathCallback.onReceiveValue(null); 
     } 
     mFilePathCallback = filePath; 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = Constant.create_file(); 
       takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
      } catch (Exception ex) { 
       // Error occurred while creating the File 
       // Log.e(TAG, "Unable to create Image File", ex); 
      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         Uri.fromFile(photoFile)); 
      } else { 
       takePictureIntent = null; 
      } 
     } 
     Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
     contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
     contentSelectionIntent.setType("image/*"); 
     Intent[] intentArray; 
     if (takePictureIntent != null) { 
      intentArray = new Intent[]{takePictureIntent}; 
     } else { 
      intentArray = new Intent[0]; 
     } 
     Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
     chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
     chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
     startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 
    } 
    // Double check that we don't have any existing callbacks 
    return true; 
} 

檢查權限添加本作棉花糖

private static boolean hasPermissions(Context context, String... permissions) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { 
     for (String permission : permissions) { 
      if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { 
       return false; 
      } 
     } 
    } 
    return true; 
} 

獲取權限結果

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 

     case REQUEST_CAMERA: { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
        // Create the File where the photo should go 
        File photoFile = null; 
        try { 
         photoFile = Constant.create_file(); 
         takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
        } catch (Exception ex) { 
        } 
        // Continue only if the File was successfully created 
        if (photoFile != null) { 
         mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
        } else { 
         takePictureIntent = null; 
        } 
       } 
       Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
       contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
       contentSelectionIntent.setType("image/*"); 
       Intent[] intentArray; 
       if (takePictureIntent != null) { 
        intentArray = new Intent[]{takePictureIntent}; 
       } else { 
        intentArray = new Intent[0]; 
       } 
       Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
       chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
       chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
       startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 

       //reload my activity with permission granted or use the features what required the permission 
      } else { 
       Patient_appointment.setWebChromeClient(new ChromeClient()); 
       Toast.makeText(mContext, "The app was not allowed to write to your storage", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 
+0

嗨,什麼是mContext?和REQUEST_CAMERA的價值?我的活動名稱是InventoryDataEditor。 –

+0

上下文mContext = InventoryDataEditor.this; private static final int REQUEST_CAMERA = 111; – user2025187