如何在Android中将图像从drawable保存为png?

时间:2017-07-23 06:42:36

标签: java android android-layout android-fragments

问题:

我无法将图片从drawable保存为android中的png。

代码:

 Bitmap b = BitmapFactory.decodeResource(getResources(), 
 eventsList.get(position).getEvent_image());

            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/png");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
                    b, "imagePath", null);
            if (!SampleUtil.isNullOrEmpty(path)) {
                /*Uri imageUri = Uri.parse(path);*/
                selectedImage = path;
            }

1 个答案:

答案 0 :(得分:3)

试试这个

步骤1.将您的位图添加到可绘制的对象中

recentArticles

第2步保存在您的存储空间

Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);

不要忘记添加 String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File file = new File(extStorageDirectory, "launcher.PNG"); try { FileOutputStream outStream = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }