【发布时间】:2014-10-03 04:10:32
【问题描述】:
你好,我有这段代码:
编辑:
imageName = data.getData();
try{
InputStream stream = getContentResolver().openInputStream(imageName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(stream,null,options);
final int REQUIRED_WIDTH=(int)screenWidth;
final int REQUIRED_HIGHT=(int)screenHeight;
int scale=1;
while(options.outWidth/scale/2>=REQUIRED_WIDTH && options.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
bitmap = BitmapFactory.decodeStream(stream, null, o2);
if ( bitmap != null ){
ok=true;
}
}catch(Exception e){
Toast.makeText(getBaseContext(),"error", Toast.LENGTH_SHORT).show();
}
但位图仍然为空
谁能告诉我为什么?或更好...如何解决?
【问题讨论】:
-
什么是
getPathFromURI()?如果是我认为的那样,请摆脱它,如aUriis not necessarily aFile。 -
我不明白。我会在这里添加方法
-
删除该方法。阅读the blog post that I linked to。在
ContentResolver上使用openInputStream()读取Uri上的数据。 -
请检查我的更新
-
此时,我不知道你在做什么。如果您的目标是在
Uri表示的数据上获取Bitmap对象,只需将stream传递给decodeStream()并去掉tmp、ByteArrayOutputStream和ByteArrayInputStream。请记住,对于您的堆空间而言,图像可能太大。
标签: android bitmapfactory