Skip to content Skip to sidebar Skip to footer

Image Uploaded From The Android App Space Seems Corrupted

In my android application, I need to upload a image in my Assets/Drawable/raw folder to the server. I tried the following: InputStream fileInputStream; if(imageChanged) {

Solution 1:

It might have to do with the buffer size? I tried two different methods to read/write a png from the assets folder and both produced a working image. I used FileOutputStream to write to the sdcard but that should not be an issue.

InputStream is, is2;
FileOutputStream out = null, out2 = null;
try {
  //method 1: compressing a Bitmap
  is = v.getContext().getAssets().open("yes.png");
  Bitmap bmp = BitmapFactory.decodeStream(is);
  String filename = Environment.getExternalStorageDirectory().toString()+File.separator+"yes.png";
  Log.d("BITMAP", filename);
  out = new FileOutputStream(filename);
  bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

  //method 2: Plain stream IO
  String filename2 = Environment.getExternalStorageDirectory().toString()+File.separator+"yes2.png";
  out2 = new FileOutputStream(filename2);
  Log.d("BITMAP", filename2);
  int r, i=0;
  is2 = v.getContext().getAssets().open("yes.png");
  while ((r = is2.read()) != -1) {
    Log.d ("OUT - byte " + i, "Value: " + r);
    out2.write(r);
    i++;
  }

} catch (IOException e) {
  e.printStackTrace();
} finally {
  try {
    if (out != null)
      out.close();
    if (out2 != null)
      out2.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

Post a Comment for "Image Uploaded From The Android App Space Seems Corrupted"