0

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?

8
  • u found any exception with this code. Commented Jan 28, 2012 at 21:12
  • Yes, when I try to use bmp[i] it gives me a NullPointerException Commented Jan 28, 2012 at 21:15
  • not sure but u have check is your file on your location or not. Commented Jan 28, 2012 at 21:27
  • in = new FileInputStream( Environment.getExternalStorageDirectory() + File.separator + "numbers" + i); what is this? Commented Jan 28, 2012 at 21:57
  • is ur bitmap here or not can u plz check it manually.. Commented Jan 28, 2012 at 21:58

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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");

8 Comments

Yes I am, now when I checket the stacktrace it stands: "Persission denied". Why?
Please post your logcat exception. It will help to figure out the issue
Also, what is the full path that you are trying to write to? Does it end up being /sdcard/mind? I've seen some cases where you may need to write to a folder on the SDCARD rather than the root.
exception:01-29 00:22:42.129: D/IO(8025): Permission denied
I have checked the path, the result should work: 01-29 00:19:44.119: D/path(7994): /mnt/sdcard/mind
|
1

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.