当用户单击此按钮时,应用程序应从网络中检索图像。它应该在一个单独的线程中执行此操作,并且当图像下载时,它应该使用ImageView显示在同一屏幕上。我似乎无法使其工作,但我觉得我真的很亲密,任何人都可以帮助我?谢谢!输入的图像URL在图像上的editText上查看
我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stephen.asyncdownloadimageurl">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/alert_light_frame"
android:layout_marginBottom="146dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:ems="10" />
我java的文件
public class MainActivity extends AppCompatActivity {
Button btn;
EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
et = (EditText) findViewById(R.id.editText);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
URL url = new URL(et.getText().toString());
new MyDownloadTask().execute(url);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(URL... params) {
URL url = params[0];
Bitmap bitmap = null;
try {
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "Failed to Download Image", Toast.LENGTH_LONG).show();
}
}
}
}
你能描述错误吗?没有结果?没有来自流的数据?有关该问题的更多信息? –
@TannerSummers我的Toast消息无法告诉我它尝试检索图像失败,但它没有得到图像。我所有的代码都运行了,但我似乎无法找到问题atm为什么它是不工作。 –
我把你的代码放到我的电脑里。并且工作正常,除了我没有把st.getText()函数转换成字符串。所以你可能想看看它是否有价值。另外如果我可能会问,为什么你从textview中读取? –