从相同的URL不显示所有图像 [英] all images not displaying from the same url
问题描述
不是所有的图像正在运行我的应用程序时显示。我从JSON得到这个结果。
{结果:[{ID:1,名:空,路径:的 http://api.androidhive.info/json/movies/1.jpg },{ID:2,名:空,路径 的http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"32","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"31","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"30","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"29","name":null,"path":" http://www.justedhak.comlu.com/images/uploaded_images.jpg } ]}
第2网址图像正确显示在应用程序但URL的其余部分不显示
这些工作
[{"id":"1","name":null,"path":"http:\api.androidhive.info\json\movies\1.jpg"},{"id":"2","name":null,"path":"http:\justedhak.comlu.com\images\uploaded_images.jpg"}
这是我的$ C $读取图像C
// showlist()下的AsyncTask prePostExecute
保护无效showList(){
尝试 {
JSONObject的jsonObj =新的JSONObject(myJSON);
人民= jsonObj.getJSONArray(TAG_RESULTS);
的for(int i = 0; I< peoples.length();我++){
JSONObject的C = peoples.getJSONObject(我);
字符串ID = c.getString(TAG_ID);
字符串URL = c.getString(TAG_PATH);
Listitem.add(新列表项(ID,网址));
}
GridViewAdapter适配器=新GridViewAdapter(这一点,R.layout.grid_item_layout,列表项);
// gridView.setAdapter(gridAdapter);
list.setAdapter(适配器);
}赶上(JSONException E){
e.printStackTrace();
}
}
公共类GetDataJSON扩展的AsyncTask<字符串,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串... PARAMS){
DefaultHttpClient的HttpClient =新DefaultHttpClient(新BasicHttpParams());
HttpPost httppost =新HttpPost(http://justedhak.comlu.com/get-data.php);
//取决于Web服务
httppost.setHeader(内容型,应用/ JSON);
的InputStream的InputStream = NULL;
字符串结果= NULL;
尝试 {
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
的InputStream = entity.getContent();
// JSON是UTF-8在默认情况下
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(InputStream中,UTF-8),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(行+\ N);
}
结果= sb.toString();
}赶上(例外五){
//哎呀
}
最后 {
尝试{如果(的InputStream!= NULL)inputStream.close();}赶上(例外挤){}
}
返回结果;
}
我想我的错误是在这里网格视图适配器
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
ViewHolder持有人;
如果(行== NULL){
LayoutInflater充气= LayoutInflater.from(mcontext);
行= inflater.inflate(layoutResourceId,父母,假);
持有人=新ViewHolder();
holder.imageTitle =(TextView中)row.findViewById(R.id.text);
holder.imageView =(ImageView的)row.findViewById(R.id.imageView);
row.setTag(保持器);
} 其他 {
支架=(ViewHolder)row.getTag();
}
列表项的项目=的getItem(位置);
的System.out.println(item.getUrl());
holder.imageTitle.setText(item.getId());
毕加索。
与(mcontext)。
负载(item.getUrl())
.placeholder(R.drawable.ic_launcher)
。适合()
。走进(holder.imageView);
返回行;
}
静态类ViewHolder {
TextView的imageTitle;
ImageView的ImageView的;
}
}
上传
公共无效上传()
{
日历thisCal = Calendar.getInstance();
thisCal.getTimeInMillis();
// android.util.Log.i时间(类,+ thisCal在millisecinds时间值);
//位图位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
// ByteArrayOutputStream流=新ByteArrayOutputStream();
// bmp.com preSS(Bitmap.Com pressFormat.PNG,90,流); // COM preSS到你想要的格式。
意向意图= getIntent();
串selectedImage = intent.getStringExtra(的ImagePath);
乌里=了fileURI Uri.parse(selectedImage);
//乌里selectedImage = intent.getData();
的System.out.println(了fileURI);
的InputStream的ImageStream = NULL;
尝试 {
的ImageStream = getContentResolver()openInputStream(了fileURI)。
}赶上(FileNotFoundException异常E){
e.printStackTrace();
}
BMP位= BitmapFactory.de codeStream(的ImageStream);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.JPEG,30,流);
字节[]的字节数组= stream.toByteArray();
点阵位图= BitmapFactory.de codeByteArray(字节数组,0,byteArray.length);
imageview.setImageBitmap(位);
INT宽度= bitmap.getWidth();
INT高= bitmap.getHeight();
的System.out.println(宽);
的System.out.println(高度);
getResizedBitmap(位图,200);
尝试 {
stream.close();
流= NULL;
}赶上(IOException异常E){
e.printStackTrace();
}
字符串image_str = Base64.en codeBytes(字节阵列);
最后的ArrayList<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(形象,image_str));
nameValuePairs.add(新BasicNameValuePair(标题,字幕));
nameValuePairs.add(新BasicNameValuePair(姓名,济));
nameValuePairs.add(新BasicNameValuePair(categorie,categorie));
线程t =新主题(新的Runnable(){
@覆盖
公共无效的run(){
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://justedhak.comlu.com/images/upload_image.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
最后弦乐the_string_response = convertResponseToString(响应);
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
Toast.makeText(AddImage.this,响应+ the_string_response,Toast.LENGTH_LONG).show();
}
});
}赶上(最终例外五){
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
Toast.makeText(AddImage.this,ERROR+ e.getMessage(),Toast.LENGTH_LONG).show();
}
});
的System.out.println(在HTTP连接错误+ e.toString());
}
}
});
t.start();
}
获取的GridView的参考样本从这里,我刚才定制和测试,加载所有的图像吧。
Item.java:
公共类项目{
字符串IMAGEURL;
字符串称号;
公共项目(字符串IMAGEURL,串题){
超();
this.imageUrl = IMAGEURL;
this.title =称号;
}
公共字符串getImageUrl(){
返回IMAGEURL;
}
公共字符串的getTitle(){
返回称号;
}
}
CustomGridViewAdapter.java:
公共类CustomGridViewAdapter扩展ArrayAdapter<项目> {
上下文语境;
INT layoutResourceId;
ArrayList的<项目>数据=新的ArrayList<>();
公共CustomGridViewAdapter(上下文的背景下,INT layoutResourceId,
ArrayList的<项目>数据) {
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
记录保持者持有人;
如果(行== NULL){
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假);
持有人=新的记录保持者();
holder.txtTitle =(TextView中)row.findViewById(R.id.item_text);
holder.imageItem =(ImageView的)row.findViewById(R.id.item_image);
row.setTag(保持器);
} 其他 {
支架=(记录保持者)row.getTag();
}
项目项目= data.get(位置);
holder.txtTitle.setText(item.getTitle());
Picasso.with(上下文).load(item.getImageUrl())到(holder.imageItem)。
返回行;
}
静态类记录保持者{
TextView的txtTitle;
ImageView的ImageItem的;
}
}
和 MainActivity.java:
customGridAdapter =新CustomGridViewAdapter(这一点,R.layout.row_grid,gridArray);
gridView.setAdapter(customGridAdapter);
字符串URL =http://justedhak.comlu.com/get-data.php;
请求队列排队= Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest =新JsonObjectRequest(Request.Method.GET,网址,空,新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的响应){
如果(响应=零和放大器;!&安培;!response.isNull(结果)){
尝试 {
JSONArray jsonArray = response.getJSONArray(结果);
如果(jsonArray =空&安培;!&安培; jsonArray.length()大于0){
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
如果(的JSONObject = NULL和放大器;!&安培;!jsonObject.isNull(路径)){
字符串的ImagePath = jsonObject.getString(路径);
如果(的ImagePath =空&安培;!&安培;!imagePath.isEmpty()){
gridArray.add(新项目(的ImagePath,BNK));
}
}
}
customGridAdapter.notifyDataSetChanged();
}
}赶上(JSONException E){
e.printStackTrace();
}
}
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
Log.e(凌空,error.toString());
}
});
queue.add(jsonObjectRequest);
其他文件,如布局......我想你已经知道
下面是截图
Not all images are displaying when running my app. I am getting from json this result
{"result":[{"id":"1","name":null,"path":"http://api.androidhive.info/json/movies/1.jpg"},{"id":"2","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"32","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"31","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"30","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"29","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"}]}
the first 2 url images are displaying correctly in the app however the rest of the url are not displaying
these are working
[{"id":"1","name":null,"path":"http:\api.androidhive.info\json\movies\1.jpg"},{"id":"2","name":null,"path":"http:\justedhak.comlu.com\images\uploaded_images.jpg"}
these is my code of reading the image
//showlist() is under asynctask prePostExecute
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String url = c.getString(TAG_PATH);
Listitem.add(new Listitem(id,url));
}
GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
// gridView.setAdapter(gridAdapter);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
I guess my error is here grid view adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.imageView = (ImageView) row.findViewById(R.id.imageView);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Listitem item = getItem(position);
System.out.println(item.getUrl());
holder.imageTitle.setText(item.getId());
Picasso.
with(mcontext).
load(item.getUrl())
.placeholder(R.drawable.ic_launcher)
.fit()
.into(holder.imageView);
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView imageView;
}
}
upload
public void upload()
{
Calendar thisCal = Calendar.getInstance();
thisCal.getTimeInMillis();
// android.util.Log.i("Time Class ", " Time value in millisecinds "+ thisCal);
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
Intent intent = getIntent();
String selectedImage= intent.getStringExtra("imagePath");
Uri fileUri = Uri.parse(selectedImage);
// Uri selectedImage = intent.getData();
System.out.println(fileUri);
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(fileUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, stream);
byte[] byteArray = stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageview.setImageBitmap(bitmap);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
System.out.println(width);
System.out.println(height);
getResizedBitmap( bitmap, 200);
try {
stream.close();
stream = null;
} catch (IOException e) {
e.printStackTrace();
}
String image_str = Base64.encodeBytes(byteArray);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("caption",caption));
nameValuePairs.add(new BasicNameValuePair("name","je"));
nameValuePairs.add(new BasicNameValuePair("categorie",categorie));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/images/upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}
});
}catch(final Exception e){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
System.out.println("Error in http connection "+e.toString());
}
}
});
t.start();
}
Getting reference for GridView sample from here, I have just customized and tested loading all your images with it.
Item.java:
public class Item {
String imageUrl;
String title;
public Item(String imageUrl, String title) {
super();
this.imageUrl = imageUrl;
this.title = title;
}
public String getImageUrl() {
return imageUrl;
}
public String getTitle() {
return title;
}
}
CustomGridViewAdapter.java:
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
Picasso.with(context).load(item.getImageUrl()).into(holder.imageItem);
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
And MainActivity.java:
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
String url = "http://justedhak.comlu.com/get-data.php";
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null && !response.isNull("result")) {
try {
JSONArray jsonArray = response.getJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject != null && !jsonObject.isNull("path")) {
String imagePath = jsonObject.getString("path");
if (imagePath != null && !imagePath.isEmpty()) {
gridArray.add(new Item(imagePath,"BNK"));
}
}
}
customGridAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
});
queue.add(jsonObjectRequest);
Other files such as layout... I think you know already
Here is the screenshot
这篇关于从相同的URL不显示所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!