您的位置:首页 > 理论基础 > 计算机网络

网络图片url或本地图片  转bitmap保存到系统相册

2016-11-04 14:56 387 查看
先将图片url或本地图片转成bitmap  然后

本地图片转Bitmap

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ceshi);

  调用该方法

public static void saveImageToGallery(Context context, Bitmap bmp) {

    // 首先保存图片  OOOO是保存图片的文件夹名字
    File appDir = new File(Environment.getExternalStorageDirectory(), "OOOO");
    if (!appDir.exists()) {

        appDir.mkdir();
    }

    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
    try {

        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    // 其次把文件插入到系统图库
    try {

        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                file.getAbsolutePath(), fileName, null);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    // 最后通知图库更新   发送更新图片的广播
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐