20

I can't figure out what's going wrong here...I've tried writing this more succicinctly, that didn't work. I put in all the extra strings after reading other suggestions with this problem. Not helping. No clue what's happening. Could it be permissions-related? AFAIK I'm trying to write to internal memory and that doesn't need special permissions?

public void outputBitmap(){ 
    String path = Environment.DIRECTORY_PICTURES.toString();
    File folder = new File(path + "/Blabla");
    String filename = new SimpleDateFormat("yyMMddHHmmss").format(Calendar.getInstance().getTime()) + ".png";
    try {
        if (!folder.exists()) {
            folder.mkdirs();
            System.out.println("Making dirs");
        }
        File myFile = new File(folder.getAbsolutePath(), filename);
        myFile.createNewFile();

        FileOutputStream out = new FileOutputStream(myFile);
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

It goes "Making dirs" every time, the directory is not staying made, or something. When it gets to myFile.createNewFile(); it gives the error message "open failed: ENOENT (No such file or directory)"

Not sure if it's related, but the information I am trying to output is from:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    myBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.RGB_565);
    Canvas pngCanvas = new Canvas(myBitmap);
    ...[some maths and stuff]
    canvas.drawLine(...);
    pngCanvas.drawLine(...);
}

I thought I should be able to use the same canvas for the bitmap, but that caused crashed, so I'm writing the same information to both canvases. So...I don't know if that's related to the issue or a totally different bad issue or what.

Been searching all kinds of questions that seemed similar, but couldn't find any solutions that worked for me. I've been trying to solve this for days now. Anyone know what's going wrong?

Thanks

user3425269
  • 249
  • 1
  • 3
  • 12

3 Answers3

19

You are not using Environment.DIRECTORY_PICTURES correctly. It is not a folder by itself, you need to use it as a parameter to getExternalStoragePublicDirectory() method. Check here : http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • 3
    Thanks so much. Changed top line to 'File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Whatever/");' and off it goes! Thanks again! – user3425269 Mar 17 '14 at 06:16
  • 3
    Oh, also when I said "I'm trying to write to internal memory and that doesn't need special permissions?" turns out that's wrong too. I didn't realise that "external" in this case means external to the program rather than the phone. So I also had to put '' into AndroidManifest.xml So, just adding this extra info in case others have the same issue. – user3425269 Mar 17 '14 at 06:19
8

Possible Issue:

Make sure you have given following required permission in your manifest file.

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

And for Marhsmallow devices, make sure Contacts Groups Permissions is granted too by device user.
Ref: http://developer.android.com/training/permissions/requesting.html

Farhan
  • 3,162
  • 3
  • 25
  • 17
1

Just change the begining of your code from this:

public void outputBitmap(){ 
    String path = Environment.DIRECTORY_PICTURES.toString();
    File folder = new File(path + "/Blabla");

To this:

public void outputBitmap(){File folder = new File(getActivity().getExternalFilesDir(null) + IMAGE_DIRECTORY + "whatever you want for your directory name");
Marcelo Gumiero
  • 1,849
  • 2
  • 14
  • 14