我现在遇到问题几天了,我无法找到解决方案。我已经在互联网上看了几个小时,但我找不到任何对我有用的答案。
我正在创建一个应用程序,我必须将图片发送到网络服务。我正在使用Fishbun访问电话库并选择一些图片。选择图片时,我使用来自Fishbun的返回路径Bitmapfactory.decodeFile()
来显示屏幕上的图片。它运作良好。此刻的道路是:
/storage/emulated/0/DCIM/Camera/IMG_20160321_044346.jpg
然后,我将路径保存在将发送到Web服务的ArrayList中。
当我在ArrayList的路径上使用BitmapFactory.decodeFile()
时,它返回null。路径与第一次完全相同,但似乎我无法对其进行两次解码
如果我重新启动应用程序并取回与第一次在fishbun中相同的图片,decodeFile()
也会返回null。这是代码....
Fishbun活动结果:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageData) {
super.onActivityResult(requestCode, resultCode, imageData);
switch (requestCode) {
case Define.ALBUM_REQUEST_CODE:
if (resultCode == RESULT_OK) {
path = imageData.getStringArrayListExtra(Define.INTENT_PATH);
int i = 0;
int error=0;
while(i<path.size()){
Bitmap bmp = BitmapFactory.decodeFile(path.get(i));
if(bmp.getWidth()<bmp.getHeight()) // bmp is not null the first time, null the second time
{
error++;
path.remove(i);
i--;
}
i++;
}
if(error>=1){
Toast.makeText(newStep7.this,R.string.rerrors, Toast.LENGTH_LONG).show();
}
if(path.size()>0) {
mainAdapter.changePath(path); //display the picture on the screen without changing the path, the method name is kinda wrong
}
break;
}
}
}
在发送我的请求之前,这是decodeFile()
:
String imgPath = rep.getImgList().get(0);
File file = new File(imgPath);
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
Log.d("startedfromthebottom", file.getAbsolutePath()); //show the same path as in the activityresult above
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //bitmap is always null
bitmap.compress(Bitmap.CompressFormat.JPEG,100,os);
当我两次解码同一个文件时,你能帮我找到为什么我的位图为空吗?
修改: 我在Android设备监视器中找到了图像文件。图像大小为0,在输出流
之后不能再使用更新:应用bwt回答后代码变化很小:
String imgPath = rep.getImgList().get(0);
File file = new File(imgPath);
AtomicFile atomicFile =
new AtomicFile(file);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = atomicFile.startWrite();
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Log.d("showmethebitmap", bitmap.toString()); //Error: bitmap is null !
oos = new ObjectOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG,0,oos);
oos.writeObject(bitmap);
oos.flush();
atomicFile.finishWrite(fos);
...
} catch (IOException e) {
atomicFile.failWrite(fos);
throw e;
} finally {
if (oos != null) oos.close();
}
答案 0 :(得分:2)
我知道问题已经得到解答,但是如果有人仍然无效,请确保该应用程序有权读取用户的文件
答案 1 :(得分:0)
您应首先加载图片,然后打开输出流,因为这会删除现有内容。
Log.d("startedfromthebottom", file.getAbsolutePath()); //show the same path as in the activityresult above
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //bitmap is always null
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG,100,os);
这有点危险,因为如果出现问题,图像就会丢失。使用AtomicFile可能是个好主意。
编辑(根据您更新的代码):
String imgPath = rep.getImgList().get(0);
File file = new File(imgPath);
AtomicFile atomicFile = new AtomicFile(file);
FileOutputStream fos = null;
try {
// read the current image
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
// open the stream (backup the current content)
// from now on (and until finishWrite/failWrite) we cannot read the file directly
fos = atomicFile.startWrite();
Log.d("showmethebitmap", bitmap.toString()); //Error: bitmap is null !
OutputStream oos = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG,0,oos);
// flush but do not close the stream (@see AtomicFile doc)
oos.flush();
// close the stream, remove the backup
atomicFile.finishWrite(fos);
...
} catch (IOException e) {
// recover the content from the backup
atomicFile.failWrite(fos);
throw e;
}