简体   繁体   English

Android-如何将画布保存到内部存储器中的图像

[英]Android - How to save a canvas to an image in internal memory

Hi I am making an Android app that allows a user to draw on screen. 嗨,我正在制作一个允许用户在屏幕上绘画的Android应用。 I have a button, when pressed I want the canvas to save as an image into the internal memory. 我有一个按钮,按下后希望将画布另存为图像到内部存储器中。 I have tried the following codes but I keep getting FileNotFoundException open failed: EACCES (permission denied) : 我尝试了以下代码,但是我一直使FileNotFoundException open failed: EACCES (permission denied)

        String path = Environment.getDataDirectory().getAbsolutePath();
        File file = new File(path+"image.png");

        Bitmap bitmap = Bitmap.createBitmap(100,200,Bitmap.Config.ARGB_8888);

        FileOutputStream ostream;

        try {
            ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.PNG, 100, ostream);
            ostream.flush();
            ostream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

尝试使用context.openFileInput(String fileName)创建fileoutputstream。

bitmap.compress(CompressFormat.PNG, 100, context.openFileInput(fileName);

in this part try to change to this: 在这部分尝试更改为:

File file = new File(path + File.separator + "image.png");

and make sure you have these in your manifest: 并确保清单中包含以下内容:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

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