Android 不显示来自同一url的所有图像
运行我的应用程序时,并非所有图像都显示。我是从这个结果中得到的 {“result”:[{“id”:“1”,“name”:null,“path”:“},{“id”:“2”,“name”:null,“path”:“},{“id”:“32”,“name”:null,“path”:“},{“id”:”31,“name”:null,“path”:“},{“id”:“30”,“name”:null,“path”:“},{“id”:“29”,“name”:null,“path”:“}” 前2个url图像在应用程序中正确显示,但url的其余部分未显示 这些都起作用了Android 不显示来自同一url的所有图像,android,Android,运行我的应用程序时,并非所有图像都显示。我是从这个结果中得到的 {“result”:[{“id”:“1”,“name”:null,“path”:“},{“id”:“2”,“name”:null,“path”:“},{“id”:“32”,“name”:null,“path”:“},{“id”:”31,“name”:null,“path”:“},{“id”:“30”,“name”:null,“path”:“},{“id”:“29”,“name”:null,“path”:“}” 前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\u images.jpg”} 这是我读取图像的代码
//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;
}
上传
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();
}
public void upload()
{
Calendar thisCal=Calendar.getInstance();
thisCal.getTimeInMillis();
//android.util.Log.i(“时间类”,“以毫秒为单位的时间值”+thisCal);
//位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器);
//ByteArrayOutputStream=新建ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG,90,stream);//压缩为所需格式。
Intent=getIntent();
字符串selectedImage=intent.getStringExtra(“图像路径”);
urifileuri=Uri.parse(selectedImage);
//Uri selectedImage=intent.getData();
System.out.println(fileUri);
InputStream imageStream=null;
试一试{
imageStream=getContentResolver().openInputStream(fileUri);
}catch(filenotfounde异常){
e、 printStackTrace();
}
位图bmp=BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream=新建ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,30,流);
byte[]byteArray=stream.toByteArray();
位图位图=位图工厂.decodeByteArray(byteArray,0,byteArray.length);
设置图像位图(位图);
int width=bitmap.getWidth();
int height=bitmap.getHeight();
系统输出打印长度(宽度);
系统输出打印LN(高度);
getResizedBitmap(位图,200);
试一试{
stream.close();
流=空;
}捕获(IOE异常){
e、 printStackTrace();
}
字符串image_str=Base64.encodeBytes(byteArray);
最终ArrayList nameValuePairs=新ArrayList();
添加(新的BasicNameValuePair(“image”,image_str));
添加(新的BasicNameValuePair(“标题”,标题));
添加(新的BasicNameValuePair(“名称”、“je”));
添加(新的BasicNameValuePair(“分类”,分类));
线程t=新线程(新的可运行线程(){
@凌驾
公开募捐{
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://justedhak.comlu.com/images/upload_image.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
最终字符串\u字符串\u响应=convertResponseToString(响应);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(AddImage.this,“Response”+字符串响应Toast.LENGTH.LONG.show();
}
});
}捕获(最终异常e){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(AddImage.this,“ERROR”+e.getMessage(),Toast.LENGTH\u LONG.show();
}
});
System.out.println(“http连接中的错误”+e.toString());
}
}
});
t、 start();
}
您应该规范您的Uri,因为这似乎是个问题
请看一下Uri类的定义。
它解决了这样的问题,重新标记为大写/小写字母
它将转换为:
Http:\justedhak.comlu.com\images\uploaded_images.jpg
到
如果你想要更多的细节,你可以看看
方案名称由以
小写字母,后跟小写字母的任意组合
信函[……]
根据我的研究,这是一个在lib的下一个版本中被修复的问题
您可以克隆库的repo并编译自己的jar或等待
我建议你看一下Glide。从毕加索迁移过来非常简单,它有更好的性能,并且可以在列表上平滑地滚动
或者尝试删除毕加索的.fit()
Picasso.
with(mcontext).
load(item.getUrl())
.placeholder(R.drawable.ic_launcher)
.into(holder.imageView);
希望它能对您有所帮助。从中获取GridView示例的参考资料,我刚刚定制并测试了如何加载您的所有图像 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;
}
}
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;
}
}
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);
CustomGridViewAdapter.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;
}
}
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;
}
}
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);
公共类CustomGridViewAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
ArrayList数据=新的ArrayList();
公共CustomGridViewAdapter(上下文上下文,int-layoutResourceId,
ArrayList数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
记录持有者;
if(行==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新记录持有者();
holder.txtTitle=(TextView)row.findViewById(R.id.item_text);
holder.imageItem=(ImageView)row.findViewById(R.id.item\u image);
row.setTag(支架);
}否则{
holder=(RecordHolder)row.getT