BitmapFactory.decodeFile return 空
BitmapFactory.decodeFile return null
我遇到了几天的问题,但找不到解决方案。我已经在互联网上寻找了几个小时,但我找不到任何适合我的答案。
我正在制作一个应用程序,我必须将图片发送到网络服务。我正在使用 Fishbun 访问 phone 画廊并挑选一些照片。选取图片时,我使用 Bitmapfactory.decodeFile()
和 Fishbun 返回的路径在屏幕上显示图片。它运作良好。此时的路径为:
/storage/emulated/0/DCIM/Camera/IMG_20160321_044346.jpg
然后,我将路径保存在将发送到网络服务的 ArrayList 中。
当我在 ArrayList 的路径上使用 BitmapFactory.decodeFile()
时,它 returns 为空。路径和第一次一模一样,不过我好像解码不了两次
如果我重新启动应用程序并取回与第一次在 fishbun 中相同的图片,则 decodeFile()
也 returns 为空。这是代码....
鱼包 activity 结果:
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,在Outputstream
后不能再使用
更新:应用 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();
}
您应该先加载图像,然后打开输出流,因为这会擦除现有内容。
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;
}
我知道问题已经得到解答,但是如果有人仍然得到 null,请确保该应用程序有权读取用户的文件
我遇到了几天的问题,但找不到解决方案。我已经在互联网上寻找了几个小时,但我找不到任何适合我的答案。
我正在制作一个应用程序,我必须将图片发送到网络服务。我正在使用 Fishbun 访问 phone 画廊并挑选一些照片。选取图片时,我使用 Bitmapfactory.decodeFile()
和 Fishbun 返回的路径在屏幕上显示图片。它运作良好。此时的路径为:
/storage/emulated/0/DCIM/Camera/IMG_20160321_044346.jpg
然后,我将路径保存在将发送到网络服务的 ArrayList 中。
当我在 ArrayList 的路径上使用 BitmapFactory.decodeFile()
时,它 returns 为空。路径和第一次一模一样,不过我好像解码不了两次
如果我重新启动应用程序并取回与第一次在 fishbun 中相同的图片,则 decodeFile()
也 returns 为空。这是代码....
鱼包 activity 结果:
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,在Outputstream
后不能再使用更新:应用 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();
}
您应该先加载图像,然后打开输出流,因为这会擦除现有内容。
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;
}
我知道问题已经得到解答,但是如果有人仍然得到 null,请确保该应用程序有权读取用户的文件