【问题标题】:Cannot get Bitmap from File无法从文件中获取位图
【发布时间】:2016-03-10 22:02:18
【问题描述】:

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

从相机获取图像

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);

这段代码正确吗?

【问题讨论】:

  • 首先确认您的图片目录中是否真的有与您的文件名相关联的图像。
  • ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, bos); byte[] data = bos.toByteArray();

标签: java android file bitmap


【解决方案1】:

您正在创建两次图像文件,可能按照我的看法,您的代码将如下所示如下:-

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

-请原谅我的糟糕解释,但如果仍然无法正常工作,您可以分享或再次寻求帮助

【讨论】:

    【解决方案2】:
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(CompressFormat.JPEG, 100, bos);
                byte[] data = bos.toByteArray();
    

    尝试像这样转换位图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 2015-07-28
      • 2023-03-19
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多