Android分享时如何调用项目工程中的图片作为分享图标

来源:互联网 发布:广州淘宝服装加工厂 编辑:程序博客网 时间:2024/06/10 02:41

在Android开发中,分享是一个常用的功能。在分享中有很多挺好玩的地方,今天就说一说如何去调用本地项目工程drawable或mipmap下面的图片资源文件。

直接上代码:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.xxxxx);//将图片资源转成BitmapFile appDir = new File(Environment.getExternalStorageDirectory(), "xxx");//创建一个文件if (!appDir.exists()) {    appDir.mkdir();}String fileName = "xxx.jpg";File file = new File(appDir, fileName);FileOutputStream fos = null;try {    fos = new FileOutputStream(file);//文件转换成字节输出流    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);//对图片进行压缩,100是压缩质量可自定义} catch (FileNotFoundException e) {    e.printStackTrace();} finally {    try {        fos.flush();        fos.close();    } catch (IOException e) {        e.printStackTrace();    }}String imagePath = file.getAbsolutePath();//获取文件绝对路径,即可作为分享路径
这样就完成了分享本地项目工程图片资源文件到各平台的功能了,希望对你有帮助。

4 0
原创粉丝点击