简体   繁体   English

如何在Android中将可绘制图像从png保存为png?

[英]How to save image from drawable as png in android?

Problem: 问题:

I can't save image from drawable as png in android. 我无法将可绘制图像从png保存为android中的png。

Code: 码:

 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;
            }

try this 尝试这个

step 1. get your bitmap in to drawable obje like this 步骤1.将您的位图放入这样的可绘制对象中

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

step 2 save in your storage like this 步骤2这样保存在您的存储中

 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();
    }

Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission. 不要忘记添加 android.permission.WRITE_EXTERNAL_STORAGE permission.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM