webview应用程序不上载图片或视频

mitkmikd  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(256)

我有一个基于网络视图的应用程序,但不幸的是,当我试图上传一个图像,它不会选择它从文件选择器,只会打开一次文件浏览器。

  1. package com.example.onlyfans;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.os.Parcelable;
  9. import android.provider.MediaStore;
  10. import android.webkit.ValueCallback;
  11. import android.webkit.WebChromeClient;
  12. import android.webkit.WebSettings;
  13. import android.webkit.WebView;
  14. import android.widget.Toast;
  15. import androidx.appcompat.app.AppCompatActivity;
  16. import java.io.File;
  17. import java.io.IOException;
  18. public class MainActivity<uri> extends AppCompatActivity {
  19. private WebView mywebView;
  20. String webURL = "http;//onlyfans.com/";
  21. public Context context;
  22. private static final String TAG = MainActivity.class.getSimpleName();
  23. private static final int FILECHOOSER_RESULTCODE = 1;
  24. private ValueCallback<uri> mUploadMessage;
  25. private Uri mCapturedImageURI = null;
  26. // the same for Android 5.0 methods only
  27. private ValueCallback<Uri[]> mFilePathCallback;
  28. private String mCameraPhotoPath;
  29. private String mCameraVideoPath;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. mywebView = findViewById(R.id.webview);
  35. mywebView.loadUrl("https://onlyfans.com");
  36. WebSettings webSettings = mywebView.getSettings();
  37. mywebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  38. webSettings.setJavaScriptEnabled(true);
  39. webSettings.setAllowFileAccess(true);
  40. webSettings.setAllowFileAccessFromFileURLs(true);
  41. webSettings.setAllowContentAccess(true);
  42. webSettings.setAllowUniversalAccessFromFileURLs(true);
  43. webSettings.setDomStorageEnabled(true);
  44. mywebView.setWebChromeClient(new WebChromeClient() {
  45. // for Lollipop, all in one
  46. public boolean onShowFileChooser(
  47. WebView webView, ValueCallback<Uri[]> filePathCallback,
  48. WebChromeClient.FileChooserParams fileChooserParams) {
  49. if (mFilePathCallback != null) {
  50. mFilePathCallback.onReceiveValue(null);
  51. }
  52. mFilePathCallback = filePathCallback;
  53. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  54. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  55. // create the file where the photo should go
  56. File photoFile = null;
  57. photoFile = createImageFile();
  58. takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
  59. // continue only if the file was successfully created
  60. if (photoFile != null) {
  61. mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
  62. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
  63. Uri.fromFile(photoFile));
  64. } else {
  65. takePictureIntent = null;
  66. }
  67. }
  68. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  69. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  70. contentSelectionIntent.setType("image/*");
  71. Intent[] intentArray;
  72. if (takePictureIntent != null) {
  73. intentArray = new Intent[]{takePictureIntent};
  74. } else {
  75. intentArray = new Intent[0];
  76. }
  77. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  78. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  79. chooserIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.image_chooser));
  80. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  81. startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
  82. return true;
  83. }
  84. // creating image files (Lollipop only)
  85. private File createImageFile() {
  86. File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DirectoryNameHere");
  87. if (!imageStorageDir.exists()) {
  88. imageStorageDir.mkdirs();
  89. }
  90. // create an image file name
  91. imageStorageDir = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
  92. return imageStorageDir;
  93. }
  94. // openFileChooser for Android 3.0+
  95. public void openFileChooser(ValueCallback<uri> uploadMsg, String acceptType) {
  96. mUploadMessage = uploadMsg;
  97. try {
  98. File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DirectoryNameHere");
  99. if (!imageStorageDir.exists()) {
  100. imageStorageDir.mkdirs();
  101. }
  102. File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
  103. mCapturedImageURI = Uri.fromFile(file); // save to the private variable
  104. final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  105. captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
  106. // captureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  107. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  108. i.addCategory(Intent.CATEGORY_OPENABLE);
  109. i.setType("image/*");
  110. Intent chooserIntent = Intent.createChooser(i, getString(R.string.image_chooser));
  111. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
  112. startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
  113. } catch (Exception e) {
  114. Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG).show();
  115. }
  116. }
  117. // openFileChooser for Android < 3.0
  118. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  119. openFileChooser((ValueCallback<uri>) uploadMsg, "");
  120. }
  121. // openFileChooser for other Android versions
  122. /* may not work on KitKat due to lack of implementation of openFileChooser() or onShowFileChooser()
  123. https://code.google.com/p/android/issues/detail?id=62220
  124. however newer versions of KitKat fixed it on some devices */
  125. public void openFileChooser(ValueCallback<uri> uploadMsg, String acceptType, String capture) {
  126. openFileChooser(uploadMsg, acceptType);
  127. }
  128. });
  129. }
  130. // return here when file selected from camera or from SD Card
  131. @Override
  132. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  133. // code for all versions except of Lollipop
  134. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  135. if (requestCode == FILECHOOSER_RESULTCODE) {
  136. if (null == this.mUploadMessage) {
  137. return;
  138. }
  139. Uri result = null;
  140. try {
  141. if (resultCode != RESULT_OK) {
  142. result = null;
  143. } else {
  144. // retrieve from the private variable if the intent is null
  145. result = data == null ? mCapturedImageURI : data.getData();
  146. }
  147. } catch (Exception e) {
  148. Toast.makeText(getApplicationContext(), "activity :" + e, Toast.LENGTH_LONG).show();
  149. }
  150. mUploadMessage.onReceiveValue((uri) result);
  151. mUploadMessage = null;
  152. }
  153. } // end of code for all versions except of Lollipop
  154. }
  155. @Override
  156. public void onBackPressed() {
  157. if (mywebView.canGoBack()) {
  158. mywebView.goBack();
  159. } else {
  160. super.onBackPressed();
  161. }
  162. }

但是如果我添加“contentSelectionContent.settype(“video/”)在contentselectionintent.set.type(“image/”)下,我可以上传视频,但不能上传图像

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题