从网站加载图片时出现问题

时间:2010-03-25 07:35:14

标签: android time loading long-integer

我是Android的新手,并开发了一个应用程序,从网站上获取图像并显示它。我让它在模拟器中工作,但不是在真正的手机中。在某些设备中,它会崩溃或需要很长的加载时间。任何人都可以帮助我或指导我改进它,因为我不确定我加载图像的方式是否正确。

以下是我用来从网络上获取图像并相应显示的代码。

if (xmlURL.length() != 0) {


 try {
    URL url = new URL(xmlURL);
     SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();

/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/*
 * Create a new ContentHandler and apply it to the
 * XML-Reader
 */
xr.setContentHandler(myExampleHandler);

/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */

/*
 * Our ExampleHandler now provides the parsed data to
 * us.
 */
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();



 } catch (Exception e) {

   }
  }

  if (s.equalsIgnoreCase("wallpapers")) {
   Context context = helloAndroid.this.getBaseContext();

   for (int j = 0; j <= myExampleHandler.filenames.size() - 1; j++) {
    if (myExampleHandler.filenames.elementAt(j).toString() != null) {
     helloAndroid.this.ed = myExampleHandler.thumbs.elementAt(j)
       .toString();
     if (helloAndroid.this.ed.length() != 0) {
      Drawable image = ImageOperations(context,
        helloAndroid.this.ed, "image.jpg");
      file_info = myExampleHandler.filenames
        .elementAt(j).toString();
      author = "\nby "
        + myExampleHandler.authors.elementAt(j)
          .toString();
      switch (j + 1) {
      case 1:
       ImageView imgView1 = new ImageView(context);
       imgView1 = (ImageView) findViewById(R.id.image1);
       if (image.getIntrinsicHeight() > 0) {
        imgView1.setImageDrawable(image);
       } else
        imgView1
          .setImageResource(R.drawable.empty_wallpaper);
       tv = (TextView) findViewById(R.id.filename1);
       tv.setText(file_info);
       tv = (TextView) findViewById(R.id.author1);
       tv.setText(author);
       imgView1
         .setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
           // Perform action on click
           Intent myIntent1 = new Intent(
             helloAndroid.this,
             galleryFile.class);
           Bundle b = new Bundle();
           b.putString("fileID",myExampleHandler.fileid.elementAt(0).toString());
           b.putString("page", "1");
           b.putString("family", s);
           b.putString("fi",myExampleHandler.folder_id.elementAt(folder).toString());
           b.putString("kw", keyword);
           myIntent1.putExtras(b);
        startActivityForResult(
             myIntent1, 0);
          }
         });
       break;
      case 2:
       ImageView imgView2 = new ImageView(context);
       imgView2 = (ImageView) findViewById(R.id.image2);
       imgView2.setImageDrawable(image);
       tv = (TextView) findViewById(R.id.filename2);
       tv.setText(file_info);
       tv = (TextView) findViewById(R.id.author2);
       tv.setText(author);
       imgView2
         .setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
           // Perform action on click
           Intent myIntent1 = new Intent(
             helloAndroid.this,
             galleryFile.class);
           Bundle b = new Bundle();
           b.putString("fileID",myExampleHandler.fileid.elementAt(1).toString());
           b.putString("page", "1");
           b.putString("family", s);
           b.putString("fi",myExampleHandler.folder_id.elementAt(folder).toString());
           b.putString("kw", keyword);
           myIntent1.putExtras(b);
        startActivityForResult(
             myIntent1, 0);
          }
         });
       break;
      case 3:
       //same code
   break;
  }
 }
}


  }
  }
    private Drawable ImageOperations(Context ctx, String url,
   String saveFilename) {
  try {
   InputStream is = (InputStream) this.fetch(url);
   Drawable d = Drawable.createFromStream(is, "src");
   return d;
  } catch (MalformedURLException e) {
   e.printStackTrace();
   return null;
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }
 }

 public Object fetch(String address) throws MalformedURLException,
   IOException {
  URL url = new URL(address);
  Object content = url.getContent();
  return content;
 }

1 个答案:

答案 0 :(得分:0)

是的,你可以从你的设备获得logcat你知道如何连接到usb并通过adb命令推送文件吗?