gpt4 book ai didi

java - 来自网络的 Android 图片

转载 作者:行者123 更新时间:2023-12-01 15:32:45 24 4
gpt4 key购买 nike

我正在开发一个简单的货币转换器应用程序,我想通过 URL 从网络上获取图像。我使用过许多教程,但似乎没有一个能够工作,即使是简单的复制和粘贴而不进行修改。我的 XML 中是否存在问题,或者只是我的 java 代码中存在愚蠢的错误?

XML imageView 位于最底部,我只是将其与默认图标图像一起放置,但当我运行应用程序时它不会显示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:gravity="center"
android:text="Currency Converter"
android:textColor="#06BD27"
android:textSize="24dp" />

<EditText
android:id="@+id/editText1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Title"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:text="Enter an Amount:" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="82dp"
android:entries="@array/To"
android:prompt="@string/ToCurrent" />

<Spinner
android:id="@+id/spinner2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/editText1"
android:layout_marginTop="23dp"
android:entries="@array/From"
android:prompt="@string/FromCurrent" />

<EditText
android:id="@+id/editText2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="18dp" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

Java 代码 createDrawableFromURL() 应该处理图像

package com.kimberly.currency_convertor;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;



import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class CurrentConvert extends Activity
{
Spinner FirstSpin, SecondSpin;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);

FirstSpin = (Spinner)findViewById(R.id.spinner1);
SecondSpin = (Spinner)findViewById(R.id.spinner2);

TextView MainTitle = (TextView) findViewById(R.id.Title);
Typeface Currency = Typeface.createFromAsset(getAssets(), "CURRENCY.TTF");
MainTitle.setTypeface(Currency);

ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));
}

private Drawable createDrawableFromURL(String urlString)
{
Drawable image = null;
try
{
URL url = new URL(urlString);
InputStream is = (InputStream)url.getContent();
image = Drawable.createFromStream(is, "src");
}
catch (MalformedURLException e)
{
// handle URL exception
image = null;
} catch (IOException e)
{
// handle InputStream exception
image = null;
}

return image;
}
}

这是我一直试图遵循的代码 http://savagelook.com/blog/android/display-images-from-the-internet-in-android

最佳答案

只需使用BufferedImage image = ImageIO.getImage(URL)

关于java - 来自网络的 Android 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9381066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com