无法写入位图到SD卡 [英] Unable to write Bitmap into SD Card

查看:172
本文介绍了无法写入位图到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个位图到SD卡(如PNG文件),查找下面的code

 档案文件=新的文件(getExternalFilesDir(空),DemoFile.png);
的OutputStream OS =新的FileOutputStream(文件);
Log.d(JS,文件名 - >中+ file.getAbsolutePath());
//文件名 - > /mnt/sdcard/Android/data/com.pocmodule/files/DemoFile.png
bmp.com preSS(Bitmap.Com pressFormat.PNG,90,OS); //这里BMP类型是位图的
os.close();
 

但我没有看到文件DemoFile.png正在SD卡创建的。别说PNG文件,我甚至不看目录com.pocmodule在SD卡可用。

我缺少什么在我的code?

解决方案
  

我安装我的设备(与选定的USB调试模式),并探讨了目录

这不勘探的文件系统。它探讨了什么是已知的 MediaStore

因此​​,首先,调用的flush(),然后 getFD()。同步(),在的FileOutputStream 在打电话之前关闭()就可以了,以确保所有字节写入到磁盘中。

然后,使用 MediaScannerConnection SCANFILE() MediaStore 有关文件。

这可能还要求你做某种形式的刷新,在你的桌面操作系统的文件浏览器窗口中看到的变化,甚至可能是拔出并重新插上设备,由于桌面操作系统可能的缓存目录内容。

I am trying to write a Bitmap into SD Card(as Png file), Find below the Code

File file = new File(getExternalFilesDir(null), "DemoFile.png");
OutputStream os = new FileOutputStream(file);
Log.d("JS", "File name -> " + file.getAbsolutePath());
//File name -> /mnt/sdcard/Android/data/com.pocmodule/files/DemoFile.png
bmp.compress(Bitmap.CompressFormat.PNG, 90, os); //here bmp is of type 'Bitmap'
os.close();

But I do not see the file 'DemoFile.png' being created in SD Card. Let alone the Png File, I do not even see the directory 'com.pocmodule' available in the SD Card.

Am I missing anything in my code?

解决方案

I mount my Device(with USB debugging mode selected) and explored the directory

That does not explore the filesystem. It explores what is known to MediaStore.

So, first, call flush(), then getFD().sync(), on your FileOutputStream before you call close() on it, to ensure that all bytes are written to disk.

Then, use MediaScannerConnection and scanFile() to teach MediaStore about your file.

This may still require you to do some sort of "refresh" in your desktop OS's file explorer window to see the change, or possibly even unplug and re-plug in your device, due to the desktop OS possibly caching "directory" contents.

这篇关于无法写入位图到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆