【问题标题】:Android: Capture and store picturesAndroid:捕获和存储图片
【发布时间】:2015-01-09 03:54:03
【问题描述】:

我正在关注this tutorial 拍摄照片、显示缩略图并将完整照片存储在仅对我的应用程序可用的本地公共存储中。

问题:尝试访问我的应用程序的本地存储时 EACCESS(权限被拒绝)

11-12 10:36:30.765    3746-3746/com.test.example.photo W/System.err﹕ java.io.IOException: open failed: EACCES (Permission denied)
11-12 10:36:30.765    3746-3746/com.test.example.photo W/System.err﹕ at java.io.File.createNewFile(File.java:948)
11-12 10:36:30.765    3746-3746/com.test.example.photo W/System.err﹕ at java.io.File.createTempFile(File.java:1013)

我查看了this question,但它似乎已经过时,因为今天没有任何解决方案可以工作。 This question 也没有提供有效的解决方案。我看到并尝试过的其他结果和解决方案似乎只是模糊相关。

我的清单权限

</application>
    <!-- PERMISSIONS -->
    <permission
        android:name="android.hardware.Camera.any"
        android:required="true" />
    <permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:required="true" />
    <!-- android:maxSdkVersion="18" seemingly does nothing-->
</manifest>

崩溃的方法

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";

    //THIS IS WHERE IT CRASHES
    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();
    return image;
}

我正在使用 i9250 Galaxy Nexus 3 手机来运行示例,因为我的模拟器没有摄像头并且会自动删除元素。我的目标 SDK 是 16,我已将我的构建工具和 Android Studio 更新到最新版本。

我觉得我在这里遗漏了一些明显的东西,因为拍照在应用程序中是如此普遍,我无法想象它不适用于每个人,但我被困住了,我很感激你的指导。我对android很陌生,我主要使用的文献是Beginning Android 4 Game Programming,Beginning Android 4 and Pro Android 4。

感谢您的宝贵时间!

【问题讨论】:

  • 我以前看过并评论过这个问题,它究竟在哪里提供了解决方案?我没看到。我尝试了各种授予权限的方法,尽管 android 应该自动让我访问我的应用程序的公共存储,但无论如何,它都会被阻止。
  • 为了澄清,在示例中使用了 Environment.getExternalStoragePublicDirectory,但这指的是 SD 卡。据我了解,使用 getExternalFilesDir 可以提供本地沙盒存储。主要区别在于您的应用无法访问它不应该访问的文件,并且当您的应用被卸载时,本地存储的所有内容都会被删除。

标签: android file-io android-permissions


【解决方案1】:

感谢大家的帮助,现在可以了!

显然我使用的是需要权限的 SD 卡存储,如 permission vs uses-permisson 中所述,而不是从 API 级别 19 开始不需要权限的本地沙盒存储。

SD卡访问,需要写权限:Environment.getExternalStoragePublicDirectory

应用的沙盒本地存储:getExternalFilesDir

我将此代码用于 API 级别 16,它应该需要很少的努力来实现和更改,但如果您遇到问题,请留言,我会尽力帮助或澄清。

大部分解释都在代码中作为注释

    //OnClick hook, requires implements View.OnClickListener to work
    public void takePicture(View v) {
        dispatchTakePictureIntent();
    }

    private void dispatchTakePictureIntent() {
        //Create intent to capture an image from the camera
        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 directory File where the photo should go, do NOT try to create the image file itself
            File photoFile = null;
            try {
                //mCurrentPhotoPath is a File outside of the methods, so all methods know the last directory for the last picture taken
                mCurrentPhotoPath = createImageFile();
                photoFile = mCurrentPhotoPath;
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                //photoFile MUST be a directory or the camera will hang on an internal
                //error and will refuse to store the picture,
                //resulting in not being able to to click accept
                //MediaStore will automatically store a jpeg for you in the specific directory and add the filename to the path
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            //unique name, can be pretty much whatever you want
            imageId = generateImageId(); 
            //Get file.jpg as bitmap from MediaStore's returned File object
            Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath.getAbsolutePath()); 
            //resize it to fit the screen
            imageBitmap = Bitmap.createScaledBitmap(imageBitmap,300,300,false); 
            //Some ImageView in your layout.xml
            ImageView imageView = (ImageView)findViewById(R.id.imageView); 
            imageView.setImageBitmap(imageBitmap);
            Bitmap thumbnail =  makeThumbnail(mCurrentPhotoPath);
            ImageView thumbnail = (ImageView)findViewById(R.id.thumbnail); 
            thumbnail.setImageBitmap(imageBitmap);
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        //completely optional subdirectory structure
        storageDir = new File(storageDir, "custom_directory");
        return storageDir;
    }    

    private Bitmap makeThumbnail(File currentPhotoPath) {
        // Get the dimensions of the View, I strongly recommend creating a <dimens> resource for dip scaled pixels
        int targetW = 45;
        int targetH = 80;

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(currentPhotoPath.getAbsolutePath(), 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 = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath.getAbsolutePath(), bmOptions);
        return bitmap;
    }

    private long generateImageId() {
        return Calendar.getInstance().getTimeInMillis();
    }

Android 5.0,API 21,将使用 Camera2 API,据我所知,所有这些都将被隐藏得很远。你可以阅读它here

【讨论】:

    【解决方案2】:

    试试这个:

    private File getDir() {
    File sdDir = Environment
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "Your_photo_dir_here");
    

    }

    然后:

     File pictureFileDir = getDir();
    
    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
    
      Log.d("TAG", "Can't create directory to save image.");
      return;
    
    }
    
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "myphoto_" + date + ".jpg";
    
    String filename = pictureFileDir.getPath() + File.separator + photoFile;
    
    File pictureFile = new File(filename);
    
    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);
      fos.write(data);
      fos.close();
    
    } catch (Exception error) {
      Log.d("TAG", "File" + filename + "not saved: "
          + error.getMessage());
    
    }
    

    【讨论】:

    • 虽然没有直接解决手头的问题,但仍然非常有用的代码。我是否可以建议对其添加评论以明确说明哪个部分发生了什么?对我而言,与其说是我这周已经阅读了 20 多个小时的相机、内容提供商和其他巫术,不如说是对其他 android 新手而言。我特别喜欢你在图片中添加自定义文件夹的地方,因为我打算在解决存储和检索问题后这样做。
    【解决方案3】:

    使用uses-permission代替权限标签 在清单中添加这个

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

    【讨论】:

    • 这实际上修复了错误垃圾邮件,但它仍然没有写入。如果我添加 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));它实际上确实写了,但是当我尝试检索缩略图时,整个应用程序崩溃了,因为附加组件中神秘的默认“数据”标签似乎被覆盖了。我会尝试修复它并发布修复的代码。
    • 我也明白,如果可能的话,我实际上应该避免使用 WRITE_EXTERNAL_STORAGE,因为我只需要我的沙盒存储,但如果没有这个权限,我就无法访问我的沙盒。我猜这是一个错误。
    • 一开始访问被拒绝,因为教程出于某种原因建议调用SD卡,这需要权限。我已经按照您的建议更改了权限,这解决了这个问题。由于安全原因,我还将 Environment.getExternalStoragePublicDirectory 更改为 getExternalFilesDir,我不喜欢在某个时候获取我可能需要的所有权限。我现在要做的是:1.捕获图像2.在屏幕上显示缩略图,将完整图片保存到图片/mydirs 3.根据点击的缩略图添加onClick加载图片的位置。
    • 这个问题是 data.getExtras("data") 在我无法控制的代码中只写了一次。我可以保存完整的图片,这会清空“数据”,导致硬空指针崩溃,或者检索缩略图而不存储图片。我要做的是 1. 存储完整图片 2. 使用内容提供程序从图片中生成和检索缩略图 3. 添加到要单击的屏幕。我也将此作为文档问题发布,因为该示例通过覆盖生成的“数据”标签而自行崩溃,而没有任何警告或解释。
    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多