【问题标题】:Bitmap turns to black when saving to storage保存到存储时位图变为黑色
【发布时间】:2014-08-11 10:24:43
【问题描述】:

每次我尝试保存图像时,它都会变成黑色,我不知道它有什么问题。

这是我的相关代码,请看一下。

private void SaveImage(Bitmap bitmap) {
    String fname = "image.png";

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    File mypath = new File(directory, fname);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • e.printStackTrace(); 上设置断点你遇到异常了吗?
  • 不,我没有遇到任何异常。

标签: android bitmap bitmapfactory android-bitmap


【解决方案1】:

我认为是由于图像的透明度,你尝试改变图片的格式或者改变位图的 alpha 值。 可以参考这个:black bitmap

【讨论】:

  • 您给我的链接包含一些错误,我无法修复它。
【解决方案2】:

终于自己修好了。

似乎如果您使用内部存储并设置Context.MODE_PRIVATE,当我尝试使用文件资源管理器打开它时,即使您具有超级用户访问权限,它也会显示黑屏。

所以我所做的就是使用这行代码通过应用程序访问文件。

ContextWrapper cw = new ContextWrapper(getApplicationContext());
ImageView image = (ImageView) findViewById(R.id.ivImage);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
try {
    File f = new File(directory, "image.png");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
    image.setImageBitmap(b);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 2016-07-14
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多