android - 来自 Android 网站的相机 Intent 不起作用 - Android

标签 android android-intent android-camera

在这一行:BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 它抛出一个 fileNotFound 异常。这是 Logcat:

01-29 23:56:12.296: E/BitmapFactory(30046): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/Pictures/JPEG_20140129_235544_1090805596.jpg: open failed: ENOENT (No such file or directory)

在setPic()里面;但是文件在启动 Intent 期间被保存并添加到图库中,所以在 onActivityResult 之前,所以它应该在那里。你看到任何问题吗?此代码取自 Android 开发者网站 http://developer.android.com/training/camera/photobasics.html

    static final int REQUEST_TAKE_PHOTO = 1001;

              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 void galleryAddPic() {
                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    File f = new File(mCurrentPhotoPath);
                    Uri contentUri = Uri.fromFile(f);
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                }

编辑下一个代码块来自 onActivityResult:

else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == -1)){
                                     // Uri selectedImage = imageUri;
                                              mProfilePicPath = mCurrentPhotoPath;

                                              mPortraitPhoto = setPic();
                                              TextView tv = (TextView) findViewById(id.ProfilePicText);
                                tv.setText(mProfilePicPath);
                                          //}
                                     // }
                          }
                  }catch(Exception ex){
                          Log.d("shkdghrfb", ex.toString());
                  }
          }

              String mCurrentPhotoPath;

              private Bitmap setPic() {
                    // Get the dimensions of the View

                    // Get the dimensions of the bitmap
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;

                    // Determine how much to scale down the image
                    //int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

                    // Decode the image file into a Bitmap sized to fill the View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = 5;
                    bmOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    //mImageView.setImageBitmap(bitmap);
                    return bitmap;
                }

              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 = "file:" + image.getAbsolutePath();
                  galleryAddPic();
                  return image;
              }

我添加的 list 权限:

<uses-permission android:name="android.permission.CAMERA" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

最佳答案

我认为文件名中的"file"不是正确的文件名,因此将其删除。将 mCurrentPhotoPath = "file:"+ image.getAbsolutePath(); 更改为 mCurrentPhotoPath = image.getAbsolutePath();。如果这只是一个 hack 修复,请告诉我。这适用于所有兼容的设备,这一点很重要。

关于android - 来自 Android 网站的相机 Intent 不起作用 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429092/

相关文章:

java - 当我尝试通过 Intent 发送字符串时出错

android - 使用相机拍照的后台服务

android - 如何使用 android camera2 api 以固定曝光时间录制 60 fps 视频

java - Android 限制 EditText 只能输入整数

android - 将对象放入包中

android 无法解析方法 setcontentview

android - 如何以编程方式在 Infinix 手机中启动 protected 应用程序 Activity ?

android - 如何通过一次安装安装两个应用程序?

android - 发送/发送短信 : how do you identify to which SMS the broadcast belongs?

java - 如何检测照片是否垂直拍摄? (我找到了方法,但行不通)