Android保存完图片,调用系统相册不显示问题解决。

2017-09-08  本文已影响0人  文艺范码农
public String save(Context context) {
        String fileName = "mosaic_"+System.currentTimeMillis()+".png";
        File file = new File(FileUtils.getMosaicDir(),fileName);
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            mBitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.flush();
            outputStream.close();

            // 其次把文件插入到系统图库
            MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),fileName,null);
            // 最后通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
            return file.getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

加入后面两行代码,就可以读取了。

上一篇 下一篇

猜你喜欢

热点阅读