Android 系统相册不可见问题

最近做一个摄像机的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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值