0
try {
FileOutputStream out = new FileOutputStream("p1");
pictureTaken.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}

and

        case R.id.open:
        ImageView im = (ImageView) findViewById(R.id.im);
        try {
            FileInputStream in = new FileInputStream("p1");
            BufferedInputStream buf = new BufferedInputStream(in);
            byte[] bitMapA= new byte[buf.available()];
            buf.read(bitMapA);
            Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
            im.setImageBitmap(bM);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;

Both try, but they don't pass in de-bug, they just got to the catch... I got most of these parts online and modified them to my needs, but even so, it all makes sense and works in my head. Just don't see why it throws the exception.

Mark
  • 700
  • 2
  • 7
  • 17

1 Answers1

2

It sounds like its on Android, and from the path you are using (p1) you are just trying to save to a file in the app's running folder. Broadly speaking you CAN NOT write to the whatever directory that is. You'll want to do something like this:

FileOutputStream out = openFileOutput ("p1", MODE_WORLD_WRITABLE);

in the first code block, and then:

FileInputStream in = openFileInput("p1");

in the second code block.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Femi is right, in Android - you'd need to use openFileInput/openFileOutput to get valid Stream objects. – Sagar Hatekar May 22 '11 at 07:36
  • Ok, thanks, I was getting confused and mislead by the http://developer.android.com site, where they say that this is the way to save and open. Once again, thanks! – Mark May 24 '11 at 06:07
  • Wait, I have one more question, what if I want to save pictures and some other files to a folder that is public (can be seen when connected with usb). Do I still used input/output streams? Also, you save a picture by compressing, what are the other ways to write (audio, video, etc). Sorry, i'm just new to Android dev =p. – Mark May 26 '11 at 04:30
  • `openFileOutput` and `openFileInput` are specifically for content that is local to the app. If you want to save files that are on the USB card then you would use `FileInputStream` and `FileOutputStream`: look at http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder for an example of writing. – Femi May 26 '11 at 05:01
  • I have this from the link you told me to look at, and yet I still don't see the file... File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/myapp/photos"); dir.mkdirs(); File file = new File(dir, "filename"); try{ Bitmap bImage = BitmapFactory.decodeFile(pictureTaken.getPath()); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); byte b[] = bytes.toByteArray(); FileOutputStream f = new FileOutputStream(file); f.write(b); f.close(); } catch... – Mark May 27 '11 at 04:41
  • Also, do you have to convert everything you want to save to a byte array first? and then turn it back when reading data? – Mark May 27 '11 at 04:45
  • You should just be able to do `bImage.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));` – Femi May 27 '11 at 04:56
  • wait, it doesn't like the Bitmap bImage = BitmapFactory.decodeFile(pictureTaken.getPath()); in the debug. Is that why pictureTaken = data.getData(); ImageView im = (ImageView) findViewById(R.id.view); im.setImageURI(pictureTaken); doesn't work? It works if I use bitmap when gettingdata, but when I try Uri (so quality isn't lost) it doesn't work... – Mark May 27 '11 at 04:57
  • Not sure, never used the Uri. – Femi May 27 '11 at 05:00
  • well, how do you getData so that the picture is bigger than a tumbnail size? – Mark May 27 '11 at 05:02