我是Android新手,想要在循环中显示图像。我成功地能够在循环外显示它但不能在循环中显示它。除了content.append(nn2 [2] +" \ n")之外,我想将这2个代码行实现到底层代码。希望你们理解:
ImageView i2 = (ImageView)findViewById(R.id.img2);
bitmap = BitmapFactory.decodeStream((new URL ("httpS://graph.facebook.com/497305810301446/picture?type=large")).openConnection().getInputStream());
i2.setImageBitmap(bitmap);
//layout4.addView(i2);
下面是我想要显示图像的循环:
Url=new URL( "http://xx.xx.xx.xx/abc/users/get_fb.json?term="+searchValue);
urlConnection = (HttpURLConnection) Url.openConnection();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
// InputStream in = urlConnection.getInputStream();
Log.d("hello","world");
//InputStreamReader isw = new InputStreamReader();
SetServerString = bufferedReader.readLine();
Log.d("hello",SetServerString);
String[] nn=SetServerString.split("name");
String nn2[];
String nn3,nn4;
// JSONObject obj = new JSONObject(SetServerString);
content.setText("");
for(int a=0;a<nn.length;a++){
// nn2=nn[a].split(",");
// nn3=nn2[0].replaceAll("\":\"", "");
nn4 = nn[a].replaceAll("[id\\:\\}\\{\\[\\]\"]", "");
nn2 = nn4.split(",");
// nn3=nn2[1].replaceAll("[\\:\\}\\{\\[\\]\"]","");
// content.append(nn2.length+" \n ");
int p=1;
for (int i = 0; i < nn2.length && nn2.length > 1; i++) {
if(p==1) {
content.append(nn2[0] + "\n");
if (nn2.length > 2) {
content.append(nn2[2] + "\n");
} else {
content.append(nn2[1] + "\n");
}
p = 0;
}
}
答案 0 :(得分:0)
使用Picasso库。它可以非常轻松地处理图像的显示,下载和缓存。
使用Picasso,您只需键入:
ERROR in Call ERP Service: ERROR: 15 ""Value cannot be null.
Parameter name: s"" in SYSPRO_BusinessObjectCall
这就是加载单张图片的全部内容。
如果您想加载许多图片,最好使用例如Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
并创建特定的适配器。之后,您可以在适配器中使用Picasso。
答案 1 :(得分:0)
你可以使用任何毕加索或通用图像加载器
添加universal-image-loader jar(谷歌和下载)
并使用方法
/**Load initially other pics**/
public void loadPic(String url)
{
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.displayImage(url, imageview, null, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
imageview.setImageResource(R.drawable.errorimg);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
imageview.setImageBitmap(loadedImage);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
imageview.setImageResource(R.drawable.defaultimg);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
}
});
}