即使用户不接受照片,照相机意图onActivityResult代码也会保存(空白)图像 [英] Camera intent onActivityResult code saves (blank) image even if photo was not accepted by user

查看:111
本文介绍了即使用户不接受照片,照相机意图onActivityResult代码也会保存(空白)图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击叉号不接受照片时,它会以与接受照片时相同的方式结束意图.它将文件保存到设备库.但这是空白.单击十字不应该意味着resultCode!= RESULT_OK吗?我还缺少一张支票吗?谢谢.这是代码.等等,我要先保存图像,然后再进行活动结果...这是一个有缺陷的系统,但是它在官方的Android Developers网站上.如果有人可以提出修复建议,我将非常感激,因为我曾经将图像保存在onActivtyResult中,并且在某些手机上无法正常工作,从而导致了异常,所以我改成了这个.

When the user clicks the cross to not accept the photo, it ends the intent in the same way it does when they accept the photo they took. It saves a file to the device gallery. But it's blank. Shouldn't clicking the cross mean that resultCode != RESULT_OK? Is there one more check I am missing? Thanks. Here's the code. Wait, I'm saving the image before activity result...this is a flawed system, but it was on the official Android Developers website. If someone can suggest a fix I would be very greatful, because I used to save the image in onActivtyResult and it did not work on some phones, causing an exception, so I changed to this.

要启动意图:

private void dispatchTakePictureIntent() {
              Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              // Ensure that there's a camera activity to handle the intent
              if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                  // Create the File where the photo should go
                  File photoFile = null;
                  try {
                      photoFile = createImageFile();
                  } catch (IOException ex) {
                      // Error occurred while creating the File
                  }
                  // Continue only if the File was successfully created
                  if (photoFile != null) {
                      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                              Uri.fromFile(photoFile));
                      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                  }
              }
          }  

private File createImageFile() throws IOException {
              // Create an image file name
              String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
              String imageFileName = "JPEG_" + timeStamp + "_";
              File storageDir = Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES);
              File image = File.createTempFile(
                  imageFileName,  /* prefix */
                  ".jpg",         /* suffix */
                  storageDir      /* directory */
              );

              // Save a file: path for use with ACTION_VIEW intents
              mCurrentPhotoPath = image.getAbsolutePath();
              ih.galleryAddPic(mCurrentPhotoPath, this.getApplicationContext());
              return image;
          }

onActivityResult中的摄影机意图案例:

The camera intent case within onActivityResult:

else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == RESULT_OK)){
                                              mProfilePicPath = mCurrentPhotoPath;
                                              mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
                                                      GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
                                                      GlobalConstants.PROFILE_PICTURE_RESOLUTION);
                                              TextView tv = (TextView) findViewById(id.ProfilePicText);
                                tv.setText(mProfilePicPath);
                          }
                  }catch(Exception ex){
                          Log.d("shkdghrfb", ex.toString());
                  }
          }

编辑:我将onActivityResult更改为此,但无济于事(空白图像此后仍在我的图库中,并且删除的值为true):

I changed onActivityResult to this, but to no avail (the blank image is still in my gallery afterwards, and the value of deleted is true):

else if (requestCode == REQUEST_TAKE_PHOTO){
                            if(resultcode == RESULT_OK){
                                File f = new File(mCurrentPhotoPath);
                                mProfilePicPath = null;
                                if (f.exists()) {
                                    if (f.length() != 0){
                                          mProfilePicPath = mCurrentPhotoPath;
                                          mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
                                                  GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
                                                  GlobalConstants.PROFILE_PICTURE_RESOLUTION);
                                          TextView tv = (TextView) findViewById(id.ProfilePicText);
                                          tv.setText(mProfilePicPath);
                                    }
                                    else {
                                    boolean deleted = f.delete();
                                    if (deleted == true){
                                    Log.d("camera0", "deleted");
                                    }
                                    else{
                                        Log.d("camera0", "not deleted");
                                    }
                                }
                            }
                        }
                        else{
                            File f = new File(mCurrentPhotoPath);
                            boolean deleted = f.delete();
                            if (deleted == true){
                            Log.d("camera", "deleted");
                            }
                            else{
                                Log.d("camera", "not deleted");
                            }
                        }
                  }
          }catch(Exception ex){
                  Log.d("shkdghrfb", ex.toString());
          }
              }catch(Exception ex){
                      Log.d("shkdghrfb", ex.toString());
              }

编辑,好吧,我相信我需要在删除后使用MediaScannerIntent扫描SD卡的适当区域,以使其显示出来,因为它现在似乎可以正常工作.

Edit Ok I believe I needed to scan the appropriate area of the SD card with a MediaScannerIntent after the delete, for it to show, as it seems to work now.

推荐答案

不是使用createImageFile()创建文件吗? 您可以保存photoFile,然后保存结果!= RESULT_OK将其删除

Aren't you creating file with createImageFile() ? You may save photoFile and on result!=RESULT_OK delete it

顺便说一句,相机应用程序(甚至是默认)可能返回错误的结果.在日志中检查它.如果是这样,那就别依赖结果了!检查创建的文件的大小.如果== 0-将其删除

By the way, camera apps(even default) may return wrong result. Check it in logs. If they do, just don't rely on result & check created file's size. If ==0 - delete it

这篇关于即使用户不接受照片,照相机意图onActivityResult代码也会保存(空白)图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆