我在写入和读取文件时遇到问题。
代码如下:
@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;
}在控制台中,我总是有:
WARNING: bitmap == nullsetPictureFile方法中的InputStream不为空(上传到web服务的工作正常),并且我在setPictureFile中没有得到任何异常。
另一方面,当我试图读取文件时,位图似乎是空的,没有异常正在上升!
我试图读取的文件大约是200-300 KB,所以它并不大,我没有耗尽内存。
有人知道这是怎么回事吗?
发布于 2015-08-05 18:27:50
解决了。到处使用byte[]而不是InputStream解决了我的问题。
public void setPictureFile(final byte[] buffer) {
String filename = "pict.png";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(buffer);
outputStream.close();
Log.d("Profile", "File stored locally.");
} catch (Exception e) {
Log.d("Profile", e.toString());
e.printStackTrace();
}
}
public Bitmap getPictureFile() {
String filename = "pict.png";
FileInputStream inputStream;
// http://stackoverflow.com/questions/11182714/bitmapfactory-example
// http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
//String filePath = getFilesDir().getAbsolutePath() + "/" + filename;
Bitmap bitmap = null;
try {
inputStream = openFileInput(filename);
byte[] reader = new byte[inputStream.available()];
if (inputStream.read(reader)!=-1) {
Log.d("Profile", "Reading from stream...");
}
Log.d("Profile", "Stream length: " + reader.length);
bitmap = BitmapFactory.decodeByteArray(reader, 0, reader.length);
//BufferedInputStream buf = new BufferedInputStream(inputStream);
//bitmap = BitmapFactory.decodeStream(inputStream);
//Bitmap bitmap = BitmapFactory.decodeFile(filename);
if (bitmap == null) {
Log.d("Profile", "WARNING: bitmap == null");
}
inputStream.close();
//if (buf != null) {
// buf.close();
//}
} catch(FileNotFoundException e) {
Log.d("Profile Exception", e.toString());
e.printStackTrace();
} catch (OutOfMemoryError e) {
Log.d("Profile Exception", e.toString());
} catch(Exception e) {
Log.d("Profile Exception", e.toString());
e.printStackTrace();
}
return bitmap;
}https://stackoverflow.com/questions/31814906
复制相似问题