简体   繁体   中英

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. 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) :

        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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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