我正在尝试在ListView上显示图像。代码:
private String[] movieURL = {"..","..",".."};
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent());
holder.image.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
这是ViewHolder类:
private static class ViewHolder {
public TextView text1;
public TextView text2;
public ImageView image;
}
但它没有显示任何内容。我认为代码还可以。和StackTrace:
12-14 07:50:54.975: D/dalvikvm(1865): GC_CONCURRENT freed 122K, 8% free 3082K/3324K, paused 4ms+28ms, total 123ms
12-14 07:50:55.175: D/dalvikvm(1865): GC_FOR_ALLOC freed 16K, 8% free 3178K/3424K, paused 40ms, total 51ms
12-14 07:50:55.195: I/dalvikvm-heap(1865): Grow heap (frag case) to 4.291MB for 1127536-byte allocation
12-14 07:50:55.286: D/dalvikvm(1865): GC_FOR_ALLOC freed <1K, 6% free 4278K/4528K, paused 84ms, total 86ms
12-14 07:50:55.395: D/dalvikvm(1865): GC_CONCURRENT freed <1K, 6% free 4279K/4528K, paused 10ms+20ms, total 118ms
12-14 07:50:55.547: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.625: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.685: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.715: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.815: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:57.125: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:57.875: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:58.736: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:59.386: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:59.945: D/skia(1865): --- SkImageDecoder::Factory returned null
答案 0 :(得分:0)
试试这个
holder.image.setImageBitmap(BitmapFactory.decodeFile(movieurl
.get(position)));
答案 1 :(得分:0)
您是否确定Bitmap
- 对象不为空?做一些记录..
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent());
if(bitmap == null) {
Log.i("Aha!", "The Bitmap was null all this time :)");
}
holder.image.setImageBitmap(bitmap);
如果您的Bitmap
为空,请检查网址是否实际指向您要提取的资源。
另外,请记住包括
<uses-permission android:name="android.permission.INTERNET"/>
在AndroidManifest.xml
中。如果不这样做会给你一个空Bitmap
而没有任何错误。
答案 2 :(得分:0)
尝试下面的代码,使用AsyncTask从网上下载图片并在imageview中显示,它将解决您的问题。
public class MainActivity extends Activity {
ImageView mImgView1;
static Bitmap bm;
ProgressDialog pd;
String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
BitmapFactory.Options bmOptions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgView1 = (ImageView) findViewById(R.id.mImgView1);
pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
"Carregando...");
new ImageDownload().execute("");
}
public class ImageDownload extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
loadBitmap(imageUrl, bmOptions);
return imageUrl;
}
protected void onPostExecute(String imageUrl) {
pd.dismiss();
if (!imageUrl.equals("")) {
mImgView1.setImageBitmap(bm);
} else {
Toast.makeText(MainActivity.this,
"Não foi possível obter resultados", Toast.LENGTH_LONG)
.show();
}
}
}
public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bm = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bm;
}
private static InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
}