- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试从 URL 下载图像以显示为 ImageView。下载是在后台使用 AsyncTask 完成的。但是,对 BitmapFactory 的 decodeStream 的调用总是返回一个空对象。我验证了为连接提供的 Url 是正确的,但似乎 BitmapFactory 无法从 HTTP 连接返回的 InputStream 中读取图像。这是下面的代码:
@Override
protected Bitmap doInBackground(String... uri) {
Bitmap bm = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(Uri.encode(uri[0]));
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String contentType = entity.getContentType().getValue();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int halfScreen = metrics.widthPixels / 2;
int photoWidth = halfScreen > 200 ? 200 : halfScreen;
if (contentType.contains("image/jpeg") || contentType.contains("image/png") || contentType.contains("image/gif")) {
bm = BitmapFactory.decodeStream(new BufferedInputStream(entity.getContent()));
if (bm.getWidth() > photoWidth)
bm = Bitmap.createScaledBitmap(bm, photoWidth, Math.round((photoWidth*bm.getHeight())/bm.getWidth()), true);
}
} catch (Exception e) {
bm = null;
}
return bm;
}
奇怪的是,完全相同的代码在 Nexus S 上运行良好,但在运行 Android 2.1-update1 的三星上却不起作用。
最佳答案
问题出在 BitmapFactory.decodeStream() 方法中。似乎此方法有一个错误,导致它在慢速连接时失败。我应用了在 http://code.google.com/p/android/issues/detail?id=6066 找到的建议.
我在下面创建了 FlushedInputStream 类:
public class FlushedInputStream extends FilterInputStream {
protected FlushedInputStream(InputStream in) {
super(in);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int onebyte = read();
if (onebyte < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
然后,在我的代码中我使用了:
bm = BitmapFactory.decodeStream(new FlushedInputStream(entity.getContent()));
代替:
bm = BitmapFactory.decodeStream(new BufferedInputStream(entity.getContent()));
关于android - 无法使用 BitmapFactory.decodeStream() 从 Url 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011967/
通过使用方法 BitmapFactory.decodeStream(Resources res,int id) 和 BitmapFactory.decodeFile(String pathName)
我的应用程序从 SD 卡解码大量位图,因此我想重用现有位图以减少 GC 工作。我看到来自 Android Training 的示例和 this video它非常适合 BitmapFactory.dec
哪种设置 ImageView 图像的方法更好(使用更少的堆空间)? imageView.setImageDrawable(Drawable.createFromPath(path)); 或
我在向 gridview 添加可变背景时遇到 OutOfMemoryError...在 final Bitmap shelfBackground = BitmapFactory.decodeResou
url="http://www.nasa.gov/sites/default/files/styles/946xvariable_height/public/ladee_spin_2_in_motio
我打算编写一个小画廊应用程序。所以我有一个带有图像的 gridview,显示的图像被存储在本地设备上。我得到的是一个类 ImageLoader,它在后台线程 (AsyncTask) 中加载特定路径中的
在我的应用程序中,我有一个文件: private File TEMP_PHOTO_FILE = new File(Environment.getExternalStorageDirectory(),
您好,我正在从名为 image.png 的 png 图像创建位图。图像的尺寸为 75(宽)x 92(高)。当我运行这段代码时: Bitmap bitmap = BitmapFactory.decode
在我的 android 应用程序中,我上传和下载 JPEG 图像。对于某些图像,BitmapFactory 对下载的图像进行下采样,我不明白为什么以及如何阻止它这样做。我在上传和下载图像时记录位图对象
我有同一张图片的两份副本。其中一个在应用程序的资源文件夹(默认可绘制对象)中,另一个在外部存储中。 我用以下代码得到了位图: // Get from storage BitmapFactory.d
嘿,我不确定为什么每次我在图库中选择图片时都会出现这个问题? 代码如下: if (v == uploadImageButton) { // below
今天我的应用程序崩溃了,出现内存不足错误。 java.lang.OutOfMemoryError in android.graphics.BitmapFactory.nativeDecodeAsset
我正在尝试从Camera.PictureCallback onPictureTaken获取Jpeg,但是当我这样做时 bitmapImage = BitmapFactory.decodeByteArr
在研究 Android 的 BitmapFactory.Options 类时,我注意到它的字段是公开可以访问和修改的。 这与一般封装规则相反,该规则规定字段应声明为 private,并且应通过 pub
这段代码给出了运行时异常,我无法捕获它。 BitmapFactory.decodeByteArray 不会返回 null 或类似的内容。 Bitmap bitmap = BitmapFactory.d
我有一个包含两列的 ArrayList,并显示下面带有文本的图像。我正在使用高质量图像,并且需要在 GridView 中以良好的质量显示这些图像,为此我使用 BitmapFactory.Options
我应该从互联网获取图像并将其显示在我的 Activity 中。为此,我使用 asynctask 类。我忘记在我的 list 文件中提及互联网权限,并且应用程序成功运行,没有给出任何异常,并且能够获取模
如何从字节数组构造原始大小的位图? byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); Bitma
我在这里从 Cursor 获取值: if (mProfileCursor.moveToFirst()) { byte[] blob = mProfileCursor.getBl
我正在尝试使用这个: BitmapFactory.decodeFile("logo.jpg") 与我的 apk 相比,我似乎没有将文件:logo.jpg 放在正确的位置。它应该去哪里? 附言我得到的错
我是一名优秀的程序员,十分优秀!