我们希望将存储在PHP服务器中的图像加载到我们的Android应用程序中。 我们已经在mysql db中存储了图像URL,我们可以在ArrayList imgNames 中获取它们,但是我们无法将它们设置到gridview中。
我们还必须保持图像的宽高比,因为它是我们想要显示的绘画图像,我们无法在静态方块中显示它。
这是我们从服务器获取图片网址的代码 - >
public class fragmentShopall extends Fragment{
public JSONObject jsonResponse;
public JSONArray jsonMainNode;
public ListView mListView;
public ArrayList<String> mylist,imgNames ;
public ArrayAdapter<String> dataAdapter;
public GridView grid;
public ListView list;
public fragmentShopall(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentshopall, container, false);
grid=(GridView) rootView.findViewById(R.id.gridView1);
list = (ListView) rootView.findViewById(R.id.listView1);
testAsynch MyTask= new testAsynch();
MyTask.execute();
return rootView;
}
class testAsynch extends AsyncTask<Void,Integer,String>
{
String readFeed;
JSONObject json;
StringBuilder builder = new StringBuilder();
protected void onPreExecute(){
Log.d("PreExceute","On pre Exceute......");
}
protected String doInBackground(Void...arg0) {
Log.d("DoINBackGround","On doInBackground...");
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
// domain intentionally obfuscated for security reasons
//HttpGet httpGet = new HttpGet("http://innobytes.in/webservices/category");
HttpGet httpGet = new HttpGet("http://10.0.2.2/pratik/category");
httpGet.setHeader("content-type", "application/json; charset=UTF-8");
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.d("onProgressUpdate","Failed to download file..........");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return builder.toString();
}
@SuppressWarnings("null")
protected void onPostExecute(String result) {
// NOTE: You can call UI Element here.
String autcElement = null;
String img;
String url="http://innobytes.in/artsgallery.in/productpic/";
imgNames = new ArrayList<String>();
mylist = new ArrayList<String>();
try{
readFeed = result;
JSONArray jsonarray = new JSONArray(readFeed);
System.out.println("OKAY_8!!");
//*********** Process each JSON Node ***********//*
int lengthJsonArr = jsonarray.length();
for (int i = 0; i < lengthJsonArr; i++)
{
System.out.println("OKAY_9!!");
//****** Get Object for each JSON node. ***********//*
JSONObject jsonChildNode = jsonarray.getJSONObject(i);
autcElement = jsonChildNode.optString("tbimageurl").toString();
imgNames.add(autcElement);
img=url+autcElement;
mylist.add(img);
System.out.println(""+img);
System.out.print(" Hi.... "+mylist.get(i));
//Picasso.with(getActivity()).load("http://innobytes.in/artsgallery.in/productpic/"+autcElement).into((Target) list);
}
}
catch (Exception e) {
e.printStackTrace();
}
// Close progress dialog
}
}
}
使用我们提取的网址并显示所有网址的最佳解决方案是什么?保持其宽高比可能像Pinterest或Flickr?
答案 0 :(得分:0)
您可以使用LazyList Loading
机制
基本上,懒惰列表加载是从SD卡或使用URL的服务器延迟加载图像。这就像点播加载图片一样。
图像可以缓存到本地SD卡或手机内存中。网址被认为是关键。如果密钥存在于SD卡中,则显示来自SD卡的图像通过从服务器下载显示图像并将其缓存到您选择的位置。可以设置缓存限制。您还可以选择自己的位置来缓存图像。缓存也可以被清除。
而不是用户等待下载大图像然后显示惰性列表,而是按需加载图像。由于图像是缓存的,因此您可以离线显示图像。
答案 1 :(得分:0)
首先创建用于初始化值的bean 像那样
public class Beanclassname {
private String mediaImageUrl;
public String getMediaImageUrl() {
return mediaImageUrl;
}
public void setMediaImageUrl(String mediaImageUrl) {
this.mediaImageUrl = mediaImageUrl;
}
}
现在将值放在bean中,然后将其添加到arraylist中并执行setadapter 像那样
bean.setMediaImageUrl(jsonChildNode.getString("key"));
arraylist.add(bean);
_adapter = new adapter(this, arraylist);
gridview.setAdapter(_adapter);
_adapter.notifyDataSetChanged();
现在你的适配器使用lazy imageloader来显示图像
答案 2 :(得分:0)
您还可以使用Android Image Loader库。
检查一下 - https://github.com/nostra13/Android-Universal-Image-Loader
使用此库的优点 -