【问题标题】:How to Save Captured image File to data/data/<package.name>/cache ?如何将捕获的图像文件保存到 data/data/<package.name>/cache ?
【发布时间】:2017-10-26 23:39:17
【问题描述】:

我需要保护我正在拍摄的图像。来自安卓Android Training 我能够拍摄全尺寸图像。但这不是我的需要。 我需要将全尺寸图像保存到缓存目录(/data/data//cache),当用户离开我的应用程序时,我将删除这些缓存文件夹。

这是我的代码,该文件在缓存目录中成功创建。但文件大小为零,没有写入图像

static final int REQUEST_TAKE_PHOTO = 1;

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) {
        File file=null;
        try {
            String fileName = "506214";
            file = File.createTempFile(fileName, null, this.getCacheDir());
        } catch (IOException e) {
            // Error while creating file
        }
        photoURI = Uri.fromFile(file);
        Toast.makeText(this, photoURI.toString(), Toast.LENGTH_SHORT).show();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK){
        camera_image.setImageURI(photoURI);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

【问题讨论】:

    标签: android caching android-camera android-file android-camera-intent


    【解决方案1】:

    我的朋友试试这个

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
        imagesFolder.mkdirs(); // <----
        File image = new File(imagesFolder, "image_001.jpg");
        Uri uriSavedImage = Uri.fromFile(image);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    

    别忘了在清单文件中添加权限

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

    【讨论】:

    • 对不起兄弟.. 没有在 data/data/myapp/cache 中创建文件
    【解决方案2】:

    如果您在Activity 类中,请使用this.getCacheDir() 作为File 的第一个参数和第二个参数作为您的文件名以存储在缓存目录中。

       File f = new File(this.getCacheDir(), "capture_"+ imageNumber++);
       try {
          f.createNewFile();
       } catch (IOException e) {
          e.printStackTrace();
       }
    

    以下是我在上传到 S3 之前捕获并保存在缓存中的方法(此代码在 onActivityResult() 中):

        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            //create a file to write bitmap data
            File f = new File(this.getCacheDir(), "capture_"+ imageNumber++);
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //Convert bitmap to byte array
            Bitmap bitmap = photo;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte[] bitmapdata = bos.toByteArray();
    
            //write the bytes in file
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(f);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 这只是保存图片的小缩略图,而不是全尺寸图片
    猜你喜欢
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多