簡體   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