我需要从 URL http://blogs.fco.gov.uk/peterbeckingham/files/2012/08/goa-beach.jpg获取图像, 这非常容易访问。这是我的一段代码,
public class MainActivity extends Activity {
ImageView templateImage;
Bitmap bm;
String templateImageURL;
byte[] binaryURL=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
templateImage=(ImageView)findViewById(R.id.templateImage);
templateImageURL="http://blogs.fco.gov.uk/peterbeckingham/files/2012/08/goa-beach.jpg";
DownloadImageTask dwnloadtask=new DownloadImageTask();
dwnloadtask.execute();
}
private class DownloadImageTask extends AsyncTask<Void, Void, Void> {
Drawable image;
protected Void doInBackground(Void... urls) {
image = ImageOperations(getApplicationContext(), templateImageURL, "image.jpg");
return null;
}
protected void onPostExecute(Void result) {
templateImage.setImageDrawable(image);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) 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;
}
}
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/templateImage" />
</RelativeLayout>
这是清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageurldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageurldemo.MainActivity"
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>
这是我得到的异常,尽管文件可用
10-04 11:00:10.394: W/System.err(17887): java.io.FileNotFoundException: http://blogs.fco.gov.uk/peterbeckingham/files/2012/08/goa-beach.jpg
10-04 11:00:10.394: W/System.err(17887): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
10-04 11:00:10.394: W/System.err(17887): at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1018)
10-04 11:00:10.394: W/System.err(17887): at java.net.URLConnection.getContent(URLConnection.java:199)
10-04 11:00:10.399: W/System.err(17887): at java.net.URL.getContent(URL.java:447)
10-04 11:00:10.399: W/System.err(17887): at com.example.imageurldemo.MainActivity$DownloadImageTask.fetch(MainActivity.java:105)
10-04 11:00:10.399: W/System.err(17887): at com.example.imageurldemo.MainActivity$DownloadImageTask.ImageOperations(MainActivity.java:91)
10-04 11:00:10.399: W/System.err(17887): at com.example.imageurldemo.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:111)
10-04 11:00:10.399: W/System.err(17887): at com.example.imageurldemo.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:1)
10-04 11:00:10.399: W/System.err(17887): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-04 11:00:10.399: W/System.err(17887): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-04 11:00:10.399: W/System.err(17887): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-04 11:00:10.399: W/System.err(17887): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-04 11:00:10.399: W/System.err(17887): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-04 11:00:10.399: W/System.err(17887): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-04 11:00:10.399: W/System.err(17887): at java.lang.Thread.run(Thread.java:856)