在Android中读取缓存的图像文件并从BitmapFactory.decode获取Null

时间:2014-10-22 16:34:46

标签: android

我正在尝试从相机中获取图像,并将其本地存储在缓存中以在 ImageView 中预览,并在需要时上传到服务器。我得到的位图是null。

以下是我的代码:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE){
            if (resultCode == RESULT_OK){
                // TODO - save the full sized image as well
                if(mCurrentPhotoPath!=null){
                    Log.i(TAG, "Image File found!");

                    Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
                    if (imageBitmap !=null){
                        Log.i(TAG, "Successfully retrieved image back.");
                    }
                    ImageView postImageView= (ImageView) getActivity().findViewById(R.id.post_image);

                    postImageView.setImageBitmap(imageBitmap);

                }
            }
        }
    }
    String mCurrentPhotoPath;

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

        Log.i(TAG, "StorageDir: "+storageDir);

        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

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

搜索我找到并尝试过的其中一个解决方案是设置BitmapFactory.Options,但它对我的情况不起作用。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

编辑:Ran getActivity()。getCacheDir()。list(),发现文件确实已保存。此外,这是保存图像的按钮:

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (cameraIntent.resolveActivity(getActivity().getPackageManager())!=null){
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageCache(getActivity());
                    } catch (IOException ex) {
                        // Error occurred while creating the File
                        Log.i(TAG, "Error Creating File!!! No Space??");
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(photoFile));
                        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
                    }
                }else{
                    Toast.makeText(getActivity(),"Please check your camera", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "No camera to run");
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

and found that the files are indeed saved。只是一个list()不会证明这一点。您自己创建了文件,因此它们已经存在长度为0.之后的字节长度是多少?我认为仍然是0. getCacheDir()为外部图片接收者提供无法访问的内部私有内存。请尝试使用getExternalCacheDir()