我的图片值显示在logcat中但不在屏幕上显示,我在2天内挣扎了吗?
public class MyFragment extends Fragment {
int mCurrentPage;
Context c;
GridView mListView;
String id, cat;
String strUrl;
TextView tvtitle;
TextView tv_id, tv_rating, tv_url;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
mCurrentPage = data.getInt("current_page", 0);
imageLoader=ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.empty_photo)
.showImageForEmptyUri(R.drawable.empty_photo)
.showImageOnFail(R.drawable.empty_photo)
.cacheInMemory(true)
.cacheOnDisc(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = null;
DownloadTask downloadTask = new DownloadTask();
v = inflater.inflate(R.layout.starters, container, false);
tv_id = (TextView) v.findViewById(R.id.tv_starter_hide_id);
tv_rating = (TextView) v.findViewById(R.id.tv_starter_hide_ratinf);
tv_url = (TextView) v.findViewById(R.id.tv_starter_hide_url);
cat = Category.Main_Cat;
Log.i("Logcat Cat1", cat);
switch (mCurrentPage) {
case 1:
Log.v("MyFragment Heap", "Max Mem in MB:"
+ (Runtime.getRuntime().maxMemory() / 1024 / 1024));
// strUrl =
// "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
//strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
strUrl="http://vaibhavtech.com/work/android/json_topdownload.php?cat=Nature";
downloadTask.execute(strUrl);
mListView = (GridView) v.findViewById(R.id.lv_countries);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(getActivity().getBaseContext(),
Starter_info.class);
i.putExtra("clicked_position", pos);
startActivity(i);
}
});
break;
case 2:
// strUrl =
// "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
strUrl="http://vaibhavtech.com/work/android/json_topdownload.php?cat=Nature";
//strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
downloadTask.execute(strUrl);
Log.v("Splash Heap", "Max Mem in MB:"
+ (Runtime.getRuntime().maxMemory() / 1024 / 1024));
mListView = (GridView) v.findViewById(R.id.lv_countries);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(getActivity().getBaseContext(),
Starter_info.class);
i.putExtra("clicked_position", pos);
startActivity(i);
}
});
break;
default:
Log.i("Cat IS", Category.Main_Cat);
// strUrl =
strUrl="http://vaibhavtech.com/work/android/json_topdownload.php?cat=Nature";
// "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
//strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
downloadTask.execute(strUrl);
Log.v("Splash Heap", "Max Mem in MB:"
+ (Runtime.getRuntime().maxMemory() / 1024 / 1024));
mListView = (GridView) v.findViewById(R.id.lv_countries);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(getActivity().getBaseContext(),
Starter_info.class);
i.putExtra("clicked_position", pos);
startActivity(i);
}
});
break;
}
return v;
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
}
return data;
}
private class DownloadTask extends AsyncTask<String, Integer, String> {
String data = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=ProgressDialog.show(getActivity(), "", "Loading...");
}
@Override
protected String doInBackground(String... url) {
try {
data = downloadUrl(url[0]);
return data;
} catch (Exception e) {
Log.d("Background Task", e.toString());
return null;
}
}
@Override
protected void onPostExecute(String result) {
if(result!=null)
{
ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
listViewLoaderTask.execute(result);
}
else
{
if(progressDialog!=null)
progressDialog.dismiss();
Log.d("Data Returns Null"," DownloadTask Terminated");
}
}
}
private class ListViewLoaderTask extends
AsyncTask<String, Void, Boolean> {
JSONObject jObject;
@Override
protected Boolean doInBackground(String... strJson) {
try {
jObject = new JSONObject(strJson[0]);
// Getting the parsed data as a List construct
StarterParser countryJsonParser = new StarterParser();
MainActivity.WALL_PAPER_RESPONSE= countryJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(progressDialog!=null)
progressDialog.dismiss();
if(result)
mListView.setAdapter(new WallPaperAdapter(MainActivity.WALL_PAPER_RESPONSE));
}
}
public class WallPaperAdapter extends BaseAdapter {
private LayoutInflater mInflater = null;
private List<HashMap<String, Object>> wall_paper_response = null;
public WallPaperAdapter(List<HashMap<String, Object>> countries) {
mInflater = LayoutInflater.from(getActivity());
this.wall_paper_response=countries;
}
public int getCount() {
return wall_paper_response.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.lv_layout,
null);
holder = new ViewHolder();
holder.txtId = (TextView) convertView
.findViewById(R.id.tv_starter_hide_id);
//holder.txtDuration = (TextView) convertView
// .findViewById(R.id.tv_starter_hide_ratinf);
holder.txtTitle= (TextView) convertView
.findViewById(R.id.tv_starter_hide_imagename);
holder.txtYear = (TextView) convertView
.findViewById(R.id.tv_starter_hide_url);
holder.imgPoster= (ImageView) convertView
.findViewById(R.id.iv_flag);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.txtId.setText(wall_paper_response.get(position)
.get("id").toString());
//holder.txtDuration.setText(wall_paper_response.get(position)
// .get("duration").toString());
holder.txtTitle.setText(wall_paper_response.get(position)
.get("title").toString());
holder.txtYear.setText(wall_paper_response.get(position)
.get("year").toString());
Log.v("Image Loader URL",wall_paper_response.get(position)
.get("flag_path").toString());
imageLoader.displayImage(wall_paper_response.get(position)
.get("flag_path").toString(),holder.imgPoster,options);
} catch (Exception e) {
}
return convertView;
}
class ViewHolder {
public TextView txtId;
// public TextView txtDuration;
public TextView txtTitle;
public TextView txtYear;
public ImageView imgPoster;
}
}
}
我正在制作壁纸应用程序,我的图像来自gridview中的json,它的值显示在logcat上但不在屏幕上显示我做什么?
答案 0 :(得分:1)
我已经检查过JSON响应,它只包含Image名称。而它应该包含图像的完整路径。然后,只有这样它才能获得图像的实际位置并加载图像。
您需要修改图像值的json响应,并提供除title
值的图像名称之外的图像的完整路径。
尝试更新您的JSON响应,如下所示:
{ "Demo": [ { "id": "1" ,"title": "http://www.vaibhavtech.com/work/android/1000.jpg" } ,
{ "id": "2" ,"title": "http://www.vaibhavtech.com/work/android/1001.jpg" } ,
{ "id": "3" ,"title": "http://www.vaibhavtech.com/work/android/1002.jpg" } ]}
答案 1 :(得分:0)
由于您的json响应包含字符串url.thus,您需要从该URL下载图像,而不仅仅是您可以显示它
试试这段代码,
你的班级
ImageView lrImageView=(ImageView)findViewById(R.id.YOUR_IMAGE_ID);
ImageLoader task = new ImageLoader()
{
@Override
protected void onPostExecute(Bitmap result)
{
lrImageView.setImageBitmap(result);
}
};
task.execute(new String[]{strUrl});
和ImageLoader类的内容是
public class ImageLoader extends AsyncTask<String,Void,Bitmap>
{
@Override
protected Bitmap doInBackground(String... urls)
{
Bitmap map = null;
for (String url : urls)
{
map = downloadImage(url);
}
return map;
}
private Bitmap downloadImage(String url)
{
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try
{
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
}
catch (IOException ex) {}
return bitmap;
}
private InputStream getHttpConnection(String urlString)throws IOException
{
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try
{
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
stream = httpConnection.getInputStream();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return stream;
}
}