【发布时间】:2014-04-12 07:53:46
【问题描述】:
所有,
我有以下问题。代码如下:
class ParseCommunicator
{
private static int width = -1;
private static int height = -1;
@SuppressWarnings("deprecation")
public void getPhoto(Device dev) throws ParseException, IOException
{
InputStream stream = null;
Bitmap bmp = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
Rect rect = new Rect();
WindowManager wm = (WindowManager) context.getSystemService( Context.WINDOW_SERVICE );
Display display = wm.getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
try
{
stream = new URL( dev.getBitmapURL() ).openStream();
opts.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream( stream, rect, opts );
int bmpHeight = opts.outHeight;
int bmpWidth = opts.outWidth;
int inSampleSize = 1;
int reqHeight = height / 3 ;
int reqWidth = width / 2;
if( bmpHeight > reqHeight || bmpWidth > reqWidth )
{
int halfHeight = bmpHeight / 2;
int halfWidth = bmpWidth / 2;
while( ( halfHeight / inSampleSize ) > reqHeight && ( halfWidth / inSampleSize ) > reqWidth )
inSampleSize *= 2;
}
opts.inSampleSize = inSampleSize;
opts.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream( stream, rect, opts );
}
catch( MalformedURLException e )
{
e.printStackTrace();
}
finally
{
if( stream != null )
stream.close();
}
if( bmp != null )
dev.setPhoto( bmp );
}
此代码应从 Parse 接口获取位图(存储为 png)。运行代码时,它没有给我一个位图,它在那个对象中有 NULL。不抛出异常。
尝试调试我发现以下内容:
如果我取出采样,读取位图没有任何问题,即注释行:
opts.inJustDecodeBounds = true;
...............
opts.inJustDecodeBounds = false;
将生成我正在寻找的位图。
这些位图稍后将在网格视图中使用。
它读取的 URL 是正确的,因为我可以在不采样的情况下毫无问题地获取位图。
当我尝试对从 Android Gallery 拍摄的图片进行采样并将其放在 ImageView 上时,相同的采样代码可以正常工作。
有人能看出发生了什么吗?
我正在使用 Android 2.2 的 LG Android 手机上进行测试。
提前谢谢你。
【问题讨论】:
-
我之前遇到过这个问题(不是 Parse)——解决方案是在第二次读取之前重置 InputStream:stackoverflow.com/a/17163300/833647
-
@KenWolf,谢谢。奇迹般有效。现在我只是想知道它是否记录在任何地方?