来自Android网站的相机意图不起作用 - Android

 百脑汇惠州店_956 发布于 2023-01-30 16:42

在这一行: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 devloper网站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;
              }

我添加的清单权限:


    

user3164083.. 5

我认为文件名中的"文件"不是正确的文件名所以删除它.改mCurrentPhotoPath = "file:" + image.getAbsolutePath();mCurrentPhotoPath = image.getAbsolutePath();.如果这只是一个黑客修复请告诉我.重要的是,这适用于所有兼容的设备.

1 个回答
  • 我认为文件名中的"文件"不是正确的文件名所以删除它.改mCurrentPhotoPath = "file:" + image.getAbsolutePath();mCurrentPhotoPath = image.getAbsolutePath();.如果这只是一个黑客修复请告诉我.重要的是,这适用于所有兼容的设备.

    2023-01-30 16:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有