I'm having a problem writing and reading files.
Here goes the code:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch(requestCode) {
case INTENT_COUNTRY:
if (data.getExtras().containsKey("country")) {
final String c = data.getStringExtra("country");
Log.d("Profile", "Country: " + c);
txtCountry.setText(c);
}
break;
case PICKER_CAMERA:
Log.d("Profile", "PICKER_CAMERA");
Bitmap bitmapCamera = (Bitmap) data.getExtras().get("data");
Bitmap thumbnailCamera = ThumbnailUtils.extractThumbnail(bitmapCamera, 320, 320);
ByteArrayOutputStream streamCamera = new ByteArrayOutputStream();
thumbnailCamera.compress(Bitmap.CompressFormat.PNG, 100, streamCamera);
byte[] byteArrayCamera = streamCamera.toByteArray();
InputStream isCamera = new ByteArrayInputStream(byteArrayCamera);
uploadPicture(isCamera);
break;
case PICKER_GALLERY:
Log.d("Profile", "PICKER_GALLERY");
Uri imageUri = data.getData();
try {
Bitmap bitmapGallery = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
Bitmap thumbnailGallery = ThumbnailUtils.extractThumbnail(bitmapGallery, 320, 320);
ByteArrayOutputStream streamGallery = new ByteArrayOutputStream();
thumbnailGallery.compress(Bitmap.CompressFormat.PNG, 100, streamGallery);
byte[] byteArrayGallery = streamGallery.toByteArray();
InputStream isGallery = new ByteArrayInputStream(byteArrayGallery);
uploadPicture(isGallery);
} catch(IOException e) {
e.printStackTrace();
}
break;
default:
Log.d("Profile", "Unmanaged request code: " + requestCode);
break;
}
}
}
public void uploadPicture(InputStream is) {
final ProgressDialog progress = new ProgressDialog(getActivity());
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setTitle(getString(R.string.loading));
progress.setMessage(getString(R.string.loading_msg));
progress.show();
/* Upload*/
AppDelegate appDelegate = (AppDelegate) getActivity().getApplication();
appDelegate.setPicture(is, new Callable<Void>() {
@Override
public Void call() throws Exception {
Log.d("Profile", "Upload ok");
progress.dismiss();
return null;
}
}, new Callable<Void>() {
@Override
public Void call() throws Exception {
Log.d("Profile", "Upload failed");
progress.dismiss();
return null;
}
});
}
public void setPictureFile(final InputStream is) {
String filename = "pict.png";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(IOUtils.toByteArray(is));
outputStream.close();
Log.d("Profile", "File stored locally.");
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap getPictureFile() {
String filename = "pict.png";
FileInputStream inputStream;
//String filePath = getFilesDir().getAbsolutePath() + "/" + filename;
Bitmap bitmap = null;
try {
inputStream = openFileInput(filename);
BufferedInputStream buf = new BufferedInputStream(inputStream);
bitmap = BitmapFactory.decodeStream(buf);
//Bitmap bitmap = BitmapFactory.decodeFile(filename);
if (bitmap == null) {
Log.d("Profile", "WARNING: bitmap == null");
}
if (inputStream != null) {
inputStream.close();
}
if (buf != null) {
buf.close();
}
} catch(FileNotFoundException e) {
Log.d("Profile", "Picture FILE not found.");
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
Log.d("Profile", "Out Of Memory");
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;
}
In console I always have:
WARNING: bitmap == null
The InputStream
in the setPictureFile
method is not null (upload to a web-services works as expected) and I didn't get any exception in setPictureFile
.
On the other hand, when I'm trying to read the file, the bitmap seems to be null, no exception is rising up!
The file I'm trying to read is about 200-300 KB, so it's not big, I'm not running out of memory.
Anyone knows what's going on?