我正在尝试从在线数据库中获取一些图像网址,并将其显示在毕加索画廊中。我的问题是我的图像没有加载。我得到了图像的边框,但它们是空的(没有显示)
到目前为止,这是我的代码:
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gridView1 = (GridView) findViewById(R.id.gridview);
image_urls = new ArrayList<>();
if (gridView1.getChildCount() != 0 || image_urls != null) {
gridView1.setAdapter(null);
}
if (image_urls.isEmpty()) {
getImages();
}
}
// getting the image URLs from database
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
photos = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < photos.length(); i++) {
JSONObject c = photos.getJSONObject(i);
String email_user = c.getString(TAG_EMAIL_USER);
String poi_name = c.getString(TAG_POI_NAME);
String photo_url = c.getString(TAG_PHOTO_URL);
if (poi_name.contains(map.markerdesc) && photo_url!=null) {
// add to the picasso gallery
photo_url="http://xxxxx.ro/poi/photos/"+photo_url;
image_urls.add(photo_url);
}
}
final ImageAdapter adapter = new ImageAdapter(this);
adapter.notifyDataSetChanged();
gridView1.setAdapter(adapter);
gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(poi_photos.this, ImageActivity.class);
intent.putExtra("url", adapter.imageUrls.get(position));
startActivity(intent);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getImages() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://xxxxxx.ro/poi/table_photo_out.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;
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
public ArrayList<String> imageUrls = poi_photos.image_urls;
public ImageAdapter(Context c) {
context = c;
}
public int getCount() {
return imageUrls.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.findViewsIn(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.updateWith(imageUrls.get(position));
return convertView;
}
class ViewHolder{
private ImageView imageView;
public void findViewsIn(View convertView) {
imageView = (ImageView) convertView.findViewById(R.id.image);
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.height = getItemWidth();
layoutParams.width = getItemWidth();
imageView.setLayoutParams(layoutParams);
}
public void updateWith(String imageUrl) {
Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.rectangle)
.resize(getItemWidth(), getItemWidth())
.centerCrop()
.into(imageView);
}
private int getItemWidth(){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point.x / context.getResources().getInteger(R.integer.numColumns); // screen width / numColumns;
}
}
}
ImageActivity
public class ImageActivity extends Activity {
private Target target;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final ImageView image = (ImageView) findViewById(R.id.image);
target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
image.setImageBitmap(bitmap);
int defaultColor = getResources().getColor(R.color.Blonde);
toolbar.setBackgroundColor(palette.getLightVibrantColor(defaultColor));
toolbar.setTitleTextColor(palette.getDarkMutedColor(defaultColor));
toolbar.setTitle(getString(R.string.app_name));
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(this)
.load(getIntent().getStringExtra("url"))
.into(target);
}
}
任何想法在这里可能有什么问题? 我使用的是毕加索库2.5.2版 任何帮助将非常感谢!
修改: 我发现它有效,如果我使用一个字符串数组而不是一个arraylist(dunno为什么)但它只有在我发送这样的网址时才有效:
public String[] imageUrls = {
"http://forum.wexphotographic.com/pictures%5Cbaby%20owl.jpg",
"https://s-media-cache-ak0.pinimg.com/236x/db/b2/4f/dbb24f70f9e9f6d95e0e103e5b5f16f7.jpg",
"http://imgs.abduzeedo.com/files/articles/baby-animals/Baby-Animals-011.jpg",
"https://s-media-cache-ak0.pinimg.com/236x/04/c6/3d/04c63d072e52affb09f4549298b754e6.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/4a/65/e1/4a65e1f4d7c72ecca33b01ce3306964f.jpg",
"http://pcwallart.com/images/cute-golden-retriever-puppies-wallpaper-1.jpg",
};
如果我将arrayList转换为字符串数组,我的照片不会上传
public String[] imageUrls = poi_photos.image_urls.toArray(new String[poi_photos.image_urls.size()]);
EDIT2 : 发现我的问题是什么......我发送的网址不正确...其中一些包含空格,所以我只修改了这部分:
public void updateWith(String imageUrl) {
imageUrl = imageUrl.replace(" ", "%20");
Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.rectangle)
.resize(getItemWidth(), getItemWidth())
.centerCrop()
.into(imageView);
}
现在它完美无缺!