(webview)捕获的图像保存在某些android版本中不起作用

fxnxkyjh  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(338)

我正在做一个webview应用程序。我在我的网站上有一个表格和一个图像上传过程是通过表格需要的。它在一些android版本上运行良好:我按file input,打开相机/多媒体资料并上传图片。在某些版本中,它将intent值返回为null(在onactivityresult中),并直接打开库而不打开相机。版本不工作(我不知道所有):安卓10,安卓7。运行版本:android9,android5。我只是一个后端网络开发人员,我不太擅长移动编程。如果我删除以下条件:“if(takePictureContent.resolveactivity(ucunco\u ekran.this.getpackagemanager())!=null)“相机已打开,但我拍摄的图像不会进入文件输入(但它保存在gallery中)。

  1. import android.Manifest;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.pm.ApplicationInfo;
  9. import android.content.pm.PackageManager;
  10. import android.content.res.Configuration;
  11. import android.net.ConnectivityManager;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.os.Environment;
  16. import android.provider.MediaStore;
  17. import android.util.Log;
  18. import android.view.KeyEvent;
  19. import android.view.View;
  20. import android.webkit.ValueCallback;
  21. import android.webkit.WebChromeClient;
  22. import android.webkit.WebSettings;
  23. import android.webkit.WebView;
  24. import android.webkit.WebViewClient;
  25. import android.widget.Button;
  26. import android.widget.Toast;
  27. import androidx.annotation.NonNull;
  28. import androidx.appcompat.app.AppCompatActivity;
  29. import androidx.core.app.ActivityCompat;
  30. import androidx.core.content.ContextCompat;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.text.SimpleDateFormat;
  34. import java.util.Date;
  35. import java.util.List;
  36. public class Ucuncu_ekran extends AppCompatActivity{
  37. WebView webView;
  38. private static final String TAG = Ucuncu_ekran.class.getSimpleName();
  39. private String mCM;
  40. private ValueCallback<Uri> mUM;
  41. private ValueCallback<Uri[]> mUMA;
  42. private final static int FCR=1;
  43. @Override
  44. protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  45. super.onActivityResult(requestCode, resultCode, intent);
  46. if(Build.VERSION.SDK_INT >= 21){
  47. Uri[] results = null;
  48. //Check if response is positive
  49. if(resultCode== Activity.RESULT_OK){
  50. if(requestCode == FCR){
  51. if(null == mUMA){
  52. return;
  53. }
  54. if(intent == null){
  55. //camera selected
  56. //Capture Photo if no image available
  57. if(mCM != null){
  58. results = new Uri[]{Uri.parse(mCM)};
  59. }
  60. }else{
  61. //gallery selected
  62. String dataString = intent.getDataString();
  63. if(dataString != null){
  64. results = new Uri[]{Uri.parse(dataString)};
  65. }
  66. }
  67. }
  68. }
  69. mUMA.onReceiveValue(results);
  70. mUMA = null;
  71. }else{
  72. if(requestCode == FCR){
  73. if(null == mUM) return;
  74. Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  75. mUM.onReceiveValue(result);
  76. mUM = null;
  77. }
  78. }
  79. }
  80. AlertDialog.Builder builder;
  81. String test = "";
  82. Button closeButton;
  83. @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
  84. @Override
  85. protected void onCreate(Bundle savedInstanceState){/*
  86. requestWindowFeature(Window.FEATURE_NO_TITLE);
  87. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  88. getSupportActionBar().hide();*/
  89. boolean b = uygulamaYukluMu("com.android.camera");
  90. super.onCreate(savedInstanceState);
  91. setContentView(R.layout.activity_ucuncu_ekran);
  92. /*
  93. closeButton = (Button) findViewById(R.id.button);
  94. builder = new AlertDialog.Builder(this);*/
  95. if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
  96. ActivityCompat.requestPermissions(Ucuncu_ekran.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
  97. }
  98. webView = (WebView) findViewById(R.id.webView2);
  99. assert webView != null;
  100. WebSettings webSettings = webView.getSettings();
  101. webSettings.setJavaScriptEnabled(true);
  102. webSettings.setAllowFileAccess(true);
  103. if(Build.VERSION.SDK_INT >= 21){
  104. webSettings.setMixedContentMode(0);
  105. webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  106. }else if(Build.VERSION.SDK_INT >= 19){
  107. webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  108. }else if(Build.VERSION.SDK_INT < 19){
  109. webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  110. }
  111. webView.setWebViewClient(new Callback());
  112. webView.loadUrl("https://mywebsite.com/");
  113. webView.setWebChromeClient(new WebChromeClient(){
  114. //For Android 3.0+
  115. public void openFileChooser(ValueCallback<Uri> uploadMsg){
  116. mUM = uploadMsg;
  117. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  118. i.addCategory(Intent.CATEGORY_OPENABLE);
  119. i.setType("*/*");
  120. Ucuncu_ekran.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FCR);
  121. }
  122. // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
  123. public void openFileChooser(ValueCallback uploadMsg, String acceptType){
  124. mUM = uploadMsg;
  125. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  126. i.addCategory(Intent.CATEGORY_OPENABLE);
  127. i.setType("*/*");
  128. Ucuncu_ekran.this.startActivityForResult(
  129. Intent.createChooser(i, "File Browser"),
  130. FCR);
  131. }
  132. //For Android 4.1+
  133. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
  134. mUM = uploadMsg;
  135. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  136. i.addCategory(Intent.CATEGORY_OPENABLE);
  137. i.setType("*/*");
  138. Ucuncu_ekran.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), Ucuncu_ekran.FCR);
  139. }
  140. //For Android 5.0+
  141. @SuppressLint("QueryPermissionsNeeded")
  142. public boolean onShowFileChooser(
  143. WebView webView, ValueCallback<Uri[]> filePathCallback,
  144. FileChooserParams fileChooserParams){
  145. if(mUMA != null){
  146. mUMA.onReceiveValue(null);
  147. }
  148. mUMA = filePathCallback;
  149. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  150. if(takePictureIntent.resolveActivity(Ucuncu_ekran.this.getPackageManager()) != null){
  151. File photoFile = null;
  152. try{
  153. photoFile = createImageFile();
  154. takePictureIntent.putExtra("PhotoPath", mCM);
  155. }catch(IOException ex){
  156. Log.e(TAG, "Failed create image", ex);
  157. }
  158. if(photoFile != null){
  159. mCM = "file:" + photoFile.getAbsolutePath();
  160. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  161. }else{
  162. takePictureIntent = null;
  163. }
  164. }
  165. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  166. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  167. contentSelectionIntent.setType("*/*");
  168. Intent[] intentArray;
  169. if(takePictureIntent != null){
  170. intentArray = new Intent[]{takePictureIntent};
  171. }else{
  172. intentArray = new Intent[0];
  173. }
  174. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  175. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  176. chooserIntent.putExtra(Intent.EXTRA_TITLE, "Select an action");
  177. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  178. startActivityForResult(chooserIntent, FCR);
  179. return true;
  180. }
  181. });
  182. }
  183. public class Callback extends WebViewClient{
  184. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
  185. Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
  186. }
  187. }
  188. // Create an image file
  189. private File createImageFile() throws IOException{
  190. @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  191. String imageFileName = "img_"+timeStamp+"_";
  192. File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  193. return File.createTempFile(imageFileName,".jpg",storageDir);
  194. }
  195. @Override
  196. public boolean onKeyDown(int keyCode, @NonNull KeyEvent event){
  197. if(event.getAction() == KeyEvent.ACTION_DOWN){
  198. switch(keyCode){
  199. case KeyEvent.KEYCODE_BACK:
  200. if(webView.canGoBack()){
  201. webView.goBack();
  202. }else{
  203. finish();
  204. }
  205. return true;
  206. }
  207. }
  208. return super.onKeyDown(keyCode, event);
  209. }
  210. @Override
  211. public void onConfigurationChanged(Configuration newConfig){
  212. super.onConfigurationChanged(newConfig);
  213. }
  214. @Override
  215. public void onBackPressed() {
  216. if (webView.canGoBack()) {
  217. webView.goBack();
  218. }
  219. else {
  220. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  221. builder.setTitle("Exit app");
  222. builder.setMessage("Are you sure?");
  223. builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
  224. public void onClick(DialogInterface dialog, int id) {
  225. }
  226. });
  227. builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
  228. public void onClick(DialogInterface dialog, int id) {
  229. Ucuncu_ekran.super.onBackPressed();
  230. }
  231. });
  232. builder.show();
  233. //super.onBackPressed();
  234. }
  235. }
  236. public boolean InternetKontrol() {
  237. ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  238. if (manager.getActiveNetworkInfo() != null && manager.getActiveNetworkInfo().isAvailable() && manager.getActiveNetworkInfo().isConnected()) {
  239. return true;
  240. }
  241. else {
  242. return false;
  243. }
  244. }
  245. }

暂无答案!

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

相关问题