SlowDeepCoder
SlowDeepCoder

Reputation: 882

saving bitmap - IO Exception

When I save my bitmap, it takes me to IOExeption, why? I dont know:

    static Bitmap[] bmp = new Bitmap[4];

public static void save(FileIO files, int location) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    try {
        File f = new File(Environment.getExternalStorageDirectory() 
                  + File.separator + "test" + File.separator + "mind");
        Log.d("path", f.toString());
        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 100, bytes);
        fo.write(bytes.toByteArray());
        fo.close();
        Log.d("fisnish", "Bitmap saved");
    } catch (IOException e) {
        Log.d("IO", e.getMessage());
    }
}

Logcat when I save:

01-29 00:53:58.020: DEBUG/path(8222): /mnt/sdcard/test/mind
01-29 00:53:58.060: DEBUG/IO(8222): Permission denied
01-29 00:53:58.240: DEBUG/dalvikvm(8222): GC_EXTERNAL_ALLOC freed 7K, 53% free 2563K/5379K, external 5375K/6713K, paused 178ms
01-29 00:54:02.029: DEBUG/dalvikvm(4723): GC_EXPLICIT freed 7K, 53% free 2643K/5511K, external 1625K/2137K, paused 3421ms

I have the user permission that I need() But it doesn't work anyway. What's the problem?

Upvotes: 0

Views: 346

Answers (3)

hooked82
hooked82

Reputation: 6376

Are you including the correct permissions? You will need the write to external storage permission.

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

Edit

Try placing the file within a directory on the SDCARD, such as:

File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "test" + File.Separator + "mind");

Upvotes: 2

Mayur Bhola
Mayur Bhola

Reputation: 735

try this scale bitmaps by using canvas.drawBitmap with providing matrix.

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Matrix m = new Matrix();
    m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
    canvas.drawBitmap(bitmap, m, new Paint());

    return output;
}

Upvotes: 2

dokkaebi
dokkaebi

Reputation: 9190

Loading sounds like a job for BitmapFactory.decodeFile. Also, I would compress directly to your file output stream:

public static void save(FileIO files, int location) {
    try {
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "numbers" + location);
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 40, fo);
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions