从相机拍摄并上传照片时出现错误。

huangapple go评论91阅读模式
英文:

Error when upload a photo tacken from the camera

问题

我是 Android Studio 和移动应用开发的初学者,但我仍在努力学习。因此,我已经成功构建了我的第一个 WebView 应用程序来显示一个网页和一张图片。我找到了许多有用的资源,帮助我完成了这个任务,但我仍然在尝试解决一个问题,即如何从手机相机(而不是图库)直接拍摄照片并上传到服务器。

1- 在我点击浏览按钮后,应用程序会提示我是要从相机还是图库中选择图片。(Android 版本 9)
2- 当我从图库上传照片时,应用程序工作正常,但是当我使用相机拍摄照片并立即上传时,上传不起作用。尽管照片名称已返回到路径字段,但它给我返回以下错误信息

> -1_net::ERR_ACESS_DENIED

以下是我的 onActivityResult 代码:

  1. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  2. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  3. // ...
  4. mUploadMessage.onReceiveValue(result);
  5. mUploadMessage = null;
  6. }
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8. // ...
  9. mFilePathCallback.onReceiveValue(results);
  10. mFilePathCallback = null;
  11. } // end of code for Lollipop only
  12. }

------ 这是 openFileChooser 的部分 ------

  1. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
  2. mUploadMessage = uploadMsg;
  3. // 设置 WebView 的一些设置
  4. try {
  5. File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DirectoryNameHere");
  6. // ...
  7. startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
  8. } catch (Exception e) {
  9. Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG).show();
  10. }
  11. }

非常感谢您的帮助。

英文:

I am a beginner to android studio and to mobile apps developing in general , however i am still trying to learn .
so I have managed to build my first webview app to show a page and a picture.I found a lot of useful resources that helped me to do so , but i am still facing a problem with uploading a photo taken from the phone camera ( not from the gallery ) directly and upload to the server .

1- after I press on the brows button the app prompts me whether to take the picture from the camera or from the gallery. ( Android version 9 )
2- when i upload a photo from the gallery the app works fine , but when I use the camera to take a photo and upload startight away it does not work. it gives me the following error message although the photo name returned to the field path

> -1_net::ERR_ACESS_DENIED

here is my onActivityResult code

  1. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  2. if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.LOLLIPOP) {
  3. if (requestCode == FILECHOOSER_RESULTCODE) {
  4. if (null == this.mUploadMessage) {
  5. return;
  6. }
  7. Uri result = null;
  8. try {
  9. if (resultCode != RESULT_OK) {
  10. result = null;
  11. } else {
  12. // retrieve from the private variable if the intent is null
  13. result = data == null ? mCapturedImageURI : data.getData();
  14. }
  15. } catch (Exception e) {
  16. Toast.makeText(getApplicationContext(), &quot;activity :&quot; + e, Toast.LENGTH_LONG).show();
  17. }
  18. mUploadMessage.onReceiveValue(result);
  19. mUploadMessage = null;
  20. }
  21. }
  22. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
  23. if (requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
  24. super.onActivityResult(requestCode, resultCode, data);
  25. return;
  26. }
  27. Uri[] results = null;
  28. if (resultCode == Activity.RESULT_OK) {
  29. if (data == null || data.getData() == null) {
  30. // if there is not data, then we may have taken a photo
  31. if (mCameraPhotoPath != null) {
  32. results = new Uri[]{Uri.parse(mCameraPhotoPath)};
  33. }
  34. } else {
  35. String dataString = data.getDataString();
  36. if (dataString != null) {
  37. results = new Uri[]{Uri.parse(dataString)};
  38. }
  39. }
  40. }
  41. mFilePathCallback.onReceiveValue(results);
  42. mFilePathCallback = null;
  43. } // end of code for Lollipop only
  44. }

-------and here is the openFileChooser ----------------

  1. public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String acceptType) {
  2. mUploadMessage = uploadMsg;
  3. mWebView.getSettings().setJavaScriptEnabled(true);
  4. mWebView.getSettings().setAllowFileAccess(true);
  5. mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
  6. mWebView.getSettings().setDomStorageEnabled(true);
  7. mWebView.getSettings().setLoadsImagesAutomatically(true);
  8. try {
  9. File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), &quot;DirectoryNameHere&quot;);
  10. if (!imageStorageDir.exists()) {
  11. imageStorageDir.mkdirs();
  12. }
  13. File file = new File(imageStorageDir + File.separator + &quot;IMG_&quot; + String.valueOf(System.currentTimeMillis()) + &quot;.jpg&quot;);
  14. mCapturedImageURI = Uri.fromFile(file); // save to the private variable
  15. final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  16. captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
  17. // captureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  18. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  19. i.addCategory(Intent.CATEGORY_OPENABLE);
  20. i.setType(&quot;image/*&quot;);
  21. Intent chooserIntent = Intent.createChooser(i, getString(R.string.image_chooser));
  22. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
  23. startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
  24. } catch (Exception e) {
  25. Toast.makeText(getBaseContext(), &quot;Camera Exception:&quot; + e, Toast.LENGTH_LONG).show();
  26. }
  27. }
  28. I really appreciate your help
  29. </details>
  30. # 答案1
  31. **得分**: 0
  32. ```java
  33. 仅在清单中放置是不够的,您必须发出请求以获取权限,
  34. if (ContextCompat.checkSelfPermission(this,
  35. Manifest.permission.REQUESTED_PERMISSION) == PackageManager.PERMISSION_GRANTED)
  36. {
  37. // 您可以使用需要权限的 API
  38. openFileChooser();
  39. } else {
  40. requestPermissions(this, new String[] {
  41. Manifest.permission.READ_EXTERNAL_STORAGE,
  42. Manifest.permission.WRITE_EXTERNAL_STORAGE,
  43. Manifest.permission.CAMERA
  44. }, 100);
  45. }
  46. @Override
  47. public void onRequestPermissionsResult(
  48. int requestCode, String[] permissions, int[] grantResults)
  49. {
  50. if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  51. openFileChooser();
  52. } else {
  53. // 未授予权限,显示提示信息或对话框
  54. }
  55. }
英文:

not enough just put in manifest,you must launch requet permission,

  1. if (ContextCompat.checkSelfPermission(this,
  2. Manifest.permission.REQUESTED_PERMISSION) ==PackageManager.PERMISSION_GRANTED)
  3. {
  4. // You can use the API that requires the permission
  5. openFileChooser();
  6. }else{
  7. requestPermissions(this,new String[] {
  8. Manifest.permission.READ_EXTERNAL_STORAGE,
  9. Manifest.permission.WRITE_EXTERNAL_STORAGE,
  10. Manifest.permission.CAMERA},100
  11. );
  12. }
  13. @Override
  14. public void onRequestPermissionsResults(
  15. int requestCode, String[] permissions,int[] grantResults)
  16. {
  17. if (requestCode==100 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED){
  18. openFileChooser();
  19. }else{
  20. //not grant show toas or dialog
  21. }
  22. }

答案2

得分: 0

我已经为我的问题找到了解决方案。
经过调试,我意识到我放在createImageFile()中的图像文件夹路径不存在且未被创建,所以我用以下代码替换了我的createImageFile(),并且它对我起作用了:

  1. String currentPhotoPath;
  2. private File createImageFile() throws IOException {
  3. // 创建图像文件名
  4. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  5. String imageFileName = "JPEG_" + timeStamp + "_";
  6. File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  7. File image = File.createTempFile(
  8. imageFileName, /* 前缀 */
  9. ".jpg", /* 后缀 */
  10. storageDir /* 目录 */
  11. );
  12. // 保存文件路径以供ACTION_VIEW意图使用
  13. currentPhotoPath = image.getAbsolutePath();
  14. return image;
  15. }

感谢大家的帮助。

英文:

I have found a solution for my problem .
after debuging I have realised that the path of the images folder that I put in the createImageFile() is not exists and not created , so i replaced my createImageFile() with the following code and it worked with me

  1. String currentPhotoPath;
  2. private File createImageFile() throws IOException {
  3. // Create an image file name
  4. String timeStamp = new SimpleDateFormat(&quot;yyyyMMdd_HHmmss&quot;).format(new Date());
  5. String imageFileName = &quot;JPEG_&quot; + timeStamp + &quot;_&quot;;
  6. File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  7. File image = File.createTempFile(
  8. imageFileName, /* prefix */
  9. &quot;.jpg&quot;, /* suffix */
  10. storageDir /* directory */
  11. );
  12. // Save a file: path for use with ACTION_VIEW intents
  13. currentPhotoPath = image.getAbsolutePath();
  14. return image;
  15. }

Thank you all for your help .

huangapple
  • 本文由 发表于 2020年9月22日 02:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63997851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定