0

私は頭の最初のアンドロイドからの日の例のNASAの画像に取り組んでいます。以下の最後のステップでは、コードはエラーを示しています。

   public void resetDisplay(String title, String date, String imageUrl, String desc){

        TextView titleView = (TextView) findViewById(R.id.ImageTitle);
        titleView.setText(title);

        TextView dateView = (TextView) findViewById(R.id.ImageDate);
        dateView.setText(date);

        ImageView imgv = (ImageView) findViewById(R.id.ImageDisplay);
        imgv.setImageBitmap(image);


        TextView descView = (TextView) findViewById(R.id.ImageDesc);
        descView.setText(desc);
    }

ImageViewセッターメソッドに問題があります。その画像変数は何ですか?この例で成功した人は誰でもガイドしてください

この例のコードを見つけることができるリンクは、非常に役立ちます!

4

2 に答える 2

1

最初に (imageUrl から) 画像をダウンロードする必要があります。

URL url = new URL(stringURL);   
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap image = BitmapFactory.decodeStream(bis);

そして、次のようにします。

imgv.setImageBitmap(image);
于 2013-03-23T19:45:29.433 に答える
-1

public class XmlHandler extends DefaultHandler {

private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss";

private boolean inUrl = false;

private boolean inTitle = false;

private boolean inDescription = false;
private boolean inItem = false;
private boolean inDate = false;
private Bitmap image = null;
private String imageUrl = null;
private String title = null;
private StringBuffer description = new StringBuffer();
private String date = null;

public void processFeed() {
    try {
        // This part is added to allow the network connection on a main GUI
        // thread...
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setContentHandler(this);
        URL urlObj = new URL(url);
        InputStream inputStream = urlObj.openConnection().getInputStream();
        reader.parse(new InputSource(inputStream));
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(new String("Got Exception General"));
    }
}

private Bitmap getBitmap(String url) {
    try {
        System.out.println(url);
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        input.close();
        return bitmap;
    } catch (IOException ioe) {
        System.out.println(new String("IOException in reading Image"));
        return null;
    } catch (Exception ioe) {
        System.out.println(new String("IOException GENERAL"));
        return null;
    }
}


public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if (localName.equals("enclosure")) {

        imageUrl = attributes.getValue("", "url");

        inUrl = true;
    } else {
        inUrl = false;
    }
    if (localName.startsWith("item")) {
        inItem = true;
    } else if (inItem) {
        if (localName.equals("title")) {

            inTitle = true;
        } else {
            inTitle = false;
        }
        if (localName.equals("description")) {
            inDescription = true;
        } else {
            inDescription = false;
        }
        if (localName.equals("pubDate")) {
            inDate = true;
        } else {
            inDate = false;
        }
    }
}


public void characters(char ch[], int start, int length) {

    String chars = new String(ch).substring(start, start + length);

    if (inUrl && image == null) {



        image = getBitmap(imageUrl);
    }
    if (inTitle && title == null) {


        title = chars;
    }
    if (inDescription && description == null ) {

        description.append(chars);
    }
    if (inDate && date == null) {

        date = chars;
    }
}

public Bitmap getImage() {
    return image;
}

public String getTitle() {
    return title;
}

public StringBuffer getDescription() {
    return description;
}

public String getDate() {
    return date;
}

}

于 2013-11-13T04:43:28.843 に答える