您的位置:首页 > 移动开发 > Android开发

Android 系统相册不可见问题

2016-04-25 20:41 453 查看
最近做一个摄像机的app,遇到这个问题,记录一下。

场景:捕获系统相机照相返回的Byte[],最终转文件图片保存到手机文件加下。

**问题:**1)系统相册中找不到上述保存的图片;2)通过windows下一步步找到手机保存图片文件夹和图片文件,找不到。3)手机文件目录可以找到存储图片的文件夹和文件夹下的大量图片。

**分析总结:**Android系统下,代码中生成的图片,需要及时更新文件夹和图片。扫面一下自己,让系统媒体库更新才可以看到这些文件和图片。

贴代码,很通用的Code:

/**
* 保存图片
*
* @param 相机返回的文件流
* @return 解析流生成的缩略图
*/
public Bitmap save(byte[] data) {
if (data != null) {
// 解析生成相机返回的图片
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
// 获取加水印的图片
// bm=getBitmapWithWaterMark(bm);
// 生成缩略图
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bm, 213, 213);
// 产生新的文件名
String imgName = FileOperateUtil.createFileNmae(".jpg");
String imagePath = mImageFolder + File.separator + imgName;
String thumbPath = mThumbnailFolder + File.separator + imgName;

File thumFile = new File(thumbPath);
File bigFile = new File(imagePath);

try {
BufferedOutputStream bg = new BufferedOutputStream(new FileOutputStream(bigFile));
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bg);
bg.flush();
bg.close();
**MediaScannerConnection.scanFile(myContext, new String[] { bigFile.getAbsolutePath() }, null, null);**

// 存图片小图
BufferedOutputStream bufferos = new BufferedOutputStream(new FileOutputStream(thumFile));
thumbnail.compress(Bitmap.CompressFormat.PNG, 50, bufferos);
bufferos.flush();
bufferos.close();

return bm;
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(getContext(), "解析相机返回流失败", Toast.LENGTH_SHORT).show();

}
} else {
Toast.makeText(getContext(), "拍照失败,请重试", Toast.LENGTH_SHORT).show();
}
return null;
}


再列举一个项目中用到的解决方案:

【场景】每次相机拍摄照片后,在相册中找不到,需要及时同步

Coding:

String imagePath=mImageFolder+File.separator+imgName;
private void updateGallery(String filename)//filename是我们的文件全名,包括后缀哦
{
MediaScannerConnection.scanFile(myContext,
new String[] { filename }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}


再具体点说,这个路径怎么写:
/storage/emulated/0/DCIM/CAMERA/20160516211552.jpg


参考Demo下载地址:

http://download.csdn.net/detail/itjavawfc/9502336


网上很多相关方面的资料,可自行度娘和google。

我参考的博客:http://droidyue.com/blog/2014/07/12/scan-media-files-in-android-chinese-edition/

需要交流可联系本人,QQ:335441537
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息