这是我第一次发帖,在输入我的头衔之后,我已经完成了所有相关问题并且仍然遇到了麻烦。 简单明了,人们将图像网址输入和EditText,他们点击一个按钮,图像出现在按钮下面。我正在使用BitmapFactory并需要保持这种方式。我还有AndroidManifest.xml已经包含相应的INTERNET权限。
Java就是这样:
package com.display.picture;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class DisplayPictureActivity extends Activity {
private EditText et;
private Button btn;
private ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et1);
iv = (ImageView) findViewById(R.id.iv1);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
URL url = new URL(et.getText().toString());
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
ImageView myImage = (ImageView) findViewById(R.id.iv1);
myImage.setImageBitmap(bitmap);
bis.close();
is.close();
}
catch (Exception e) {
Toast toast = Toast.makeText(getBaseContext(), "Failed to Download Image", Toast.LENGTH_LONG);
toast.show();
}
Bitmap image = null;
iv.setImageBitmap(image);
}
});
}
}
我在这里看到的大部分问题涉及硬编码的超链接。在这里,我应该允许用户输入图像网址然后通过位图传递,并在按下按钮后在图像视图中显示它
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.display.picture"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DisplayPictureActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
确保您在清单文件中设置了互联网权限
<uses-permission android:name="android.permission.INTERNET"/>
您正在尝试关闭输入流和使用输入流的缓冲输入流,删除其中一个关闭将修复抛出的异常。
您可能还想使用AsyncTask来执行下载,因为在下载过程中阻止UI通常不是一个好主意。
以下是修改后的代码以使用AsyncTask下载:
public class DisplayPictureActivity extends Activity {
private Button btn;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
URL url = new URL(et.getText().toString());
//URL url = new URL("http://farm7.static.flickr.com/6138/5935946400_934994190e_s_d.jpg");
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(); THIS IS THE BROKEN LINE
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "Failed to Download Image", Toast.LENGTH_LONG).show();
}
}
}
}