我們希望將存儲在PHP服務器中的圖片加載到我們的android應用程序中。 我們已經將圖像網址存儲在mysql數據庫中,我們可以將它們全部提取到ArrayList中,但我們無法將它們設置到gridview中。如何將一堆圖片加載到服務器中存儲到Android應用程序中
我們還必須保持圖像的長寬比,因爲它的繪畫圖像我們無法顯示,我們無法在靜態方塊中顯示它。
這是我們的代碼中,我們從服務器獲取圖像的URL - >
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
}
}
}
什麼是最佳的解決方案使用我們擷取到的URL來獲取圖像,並顯示它們,而保持其縱橫比也許像Pinterest或Flickr?
設置適配器從哪裏存儲圖像URL在數據庫..等待我會給你的例子 –
當然,一個例子會很好。 –