我需要从网址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)
答案 0 :(得分:0)
为什么你要做这种漫长的过程?当Android为我们提供了一个很好的工作。请参阅下面的答案,它将非常有用并且可以与之合作。
我可以推荐一种不同于魅力的方式:Android查询。
您可以从此处下载该jar文件:http://code.google.com/p/android-query/downloads/list
AQuery androidAQuery=new AQuery(this);
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
它非常快速和准确,使用它你可以在加载时找到更多功能,如动画;如果需要,获取位图;等
答案 1 :(得分:0)
尝试以下代码从网址下载图片。
// DownloadImage AsyncTask private class DownloadImage extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { super.onPreExecute(); // Create a progressdialog mProgressDialog = new ProgressDialog(MainActivity.this); // Set progressdialog title mProgressDialog.setTitle("Downloading Image.... "); // Set progressdialog message mProgressDialog.setMessage("Loading..."); mProgressDialog.setIndeterminate(false); // Show progressdialog mProgressDialog.show(); } @Override protected Bitmap doInBackground(String... URL) { String imageURL = URL[0]; Bitmap bitmap = null; try { // Download Image from URL InputStream input = new java.net.URL(imageURL).openStream(); // Decode Bitmap bitmap = BitmapFactory.decodeStream(input); } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { // Set the bitmap into ImageView image.setImageBitmap(result); // Close progressdialog mProgressDialog.dismiss(); } } }
使用以上代码:
templateImageURL="http://blogs.fco.gov.uk/peterbeckingham/files/2012/08/goa-beach.jpg"; // Execute DownloadImage AsyncTask new DownloadImage().execute(templateImageURL);
另外,为了从网址加载图片,我们首先需要将图片存储在临时位置,一旦下载,就应该将其添加到ImageView
。要创建临时文件,我们需要访问设备外部存储。为AndroidManifest.xml
文件添加所需权限。不需要ACCESS_NETWORK_STATE
权限。
To access external storage required permission is – WRITE_EXTERNAL_STORAGE