Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, save view image as png file

Tags:

android

bitmap

I was expecting the code below to save an image to my local sdcard but when I run the app and trigger the saveCanvasImage() method it's not doing so. When I look in the LogCat I can find a System.err entry. The text says:

java.io.FileNotFoundException: /mnt/sdcard/drawPic1.png: open failed: EACCES (permission denied)

I thought this would create a new png file and save it in the directory. I'm obviously wrong. What can I change here to make it work?

public void saveCanvasImage() {

    Log.d("bitmap","strm");
    tv.setDrawingCacheEnabled(true);
    Bitmap bm = tv.getDrawingCache();

    File fPath = Environment.getExternalStorageDirectory();
    File f = null;
    f = new File(fPath, "drawPic1.png");

    try {
    FileOutputStream strm = new FileOutputStream(f);
    bm.compress(CompressFormat.PNG, 80, strm);
    strm.close();
    }
    catch (IOException e){
        e.printStackTrace();
    }

}
like image 711
acl77 Avatar asked Dec 30 '13 11:12

acl77


2 Answers

Did you put permission ?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 111
Hardik Joshi Avatar answered Sep 27 '22 19:09

Hardik Joshi


are you sure you have added this permission to write to external storage

android.permission.WRITE_EXTERNAL_STORAGE
like image 31
M D Avatar answered Sep 27 '22 19:09

M D