繁体   English   中英

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

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

问题:

我无法将可绘制图像从png保存为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.将您的位图放入这样的可绘制对象中

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

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

不要忘记添加 android.permission.WRITE_EXTERNAL_STORAGE permission.

暂无
暂无

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

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