无法从文件中获取位图

时间:2015-12-06 05:11:42

标签: java android file bitmap

我无法从我编写位图的文件中检索位图。 没有显示错误。但图像显示不正确。 我将位图转换为字节,然后存储在内部缓存

从相机获取图像

Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(
        Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
        "pic.jpg");
img = Uri.fromFile(photo);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, img);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

将图像写入文件

                Bitmap takenImage =BitmapFactory.decodeFile(img.getPath());   
File folder = getCacheDir();
                    File myFile = new File(folder, "myImage.bmp");
                    FileOutputStream fos = new FileOutputStream(myFile);
                    FileInputStream fis = new FileInputStream(img.getPath());
                    byte[] image = new byte[fis.available()];
                    fos.write(image);
                    fos.flush();
                    fos.close();
                    fis.close();

读取位图

File folder = getCacheDir();
File myFile = new File(folder, "myImage.bmp");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);

这段代码是否正确?

2 个答案:

答案 0 :(得分:0)

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();

尝试像这样转换位图

答案 1 :(得分:0)

您正在创建图像文件两次,根据我的意见,您的代码将如下所示: -

File myFile = new File( Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
    "pic.jpg");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);

- 请原谅我糟糕的解释,但如果仍然无法解决,你可以分享或再次询问帮助