我正在尝试使用此网址中的图片: http://www.bisonsoftware.us/hhs/image/1.png
我想拍摄这张图片并将其设置在imageview中,我只是想找到最简单的方法。
我试过这个解决方案,但它似乎对我不起作用,它给出了一个错误说malformedurlexception: Load image from url
答案 0 :(得分:2)
在您的项目中添加此库之后。 在必要的地方写下这段代码
ImageLoader imageloader;
DisplayImageOptions options;
imageloader = ImageLoader.getInstance();
String url="http://www.bisonsoftware.us/hhs/image/1.png";
imageloader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
.displayer(new FadeInBitmapDisplayer(300)).build();
imageloader.displayImage(url,imageview);
答案 1 :(得分:1)
使用Picasso库https://square.github.io/picasso/
就像这样:
Picasso.with(context).load("http://www.bisonsoftware.us/hhs/image/1.png").into(imageView);
答案 2 :(得分:1)
如果您只想使用原生Android代码,可以使用AsyncTask
下载,然后在onPostExecute
中设置。
将其命名为:
//pass image view while creating AskyncTask object and pass url as parameter
new DownloadImageTask((ImageView) findViewById(R.id.yourImageView)).execute("your_url_here");
AsynTask致电:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
我刚刚看到你在问题中提到的网址中也提到了类似的答案。这适用于我的应用。如果它给出了MalformedUrlException
,那么您可以尝试检查url是否正确,如果需要,可以正确编码。
答案 3 :(得分:0)
你可以像Shobhit描述的那样艰难地做,或者有一个库为你做同样的事情(创建另一个渲染图像的线程),例如前面提到的毕加索或UniversalImageLoader:https://github.com/nostra13/Android-Universal-Image-Loader
答案 4 :(得分:0)
public class MainActivity extends Activity {
private Bitmap bitmap;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView1);
new downloadimage().execute();
}
private class downloadimage extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
URL url;
try {
url = new URL("http://www.bisonsoftware.us/hhs/image/1.png");
bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
imageView.setImageBitmap(bitmap);
}
}
}
这对我有用。
答案 5 :(得分:0)
尝试这样实现。它为我工作从url下载图像。
并且不要忘记在清单文件中添加Internet权限,例如 - <uses-permission android:name="android.permission.INTERNET"/>
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
ImageView imageView;
Bitmap map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
new loadImage().execute();
}
public class loadImage extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
map = downloadImage("http://www.bisonsoftware.us/hhs/image/1.png");
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
imageView.setImageBitmap(map);
}
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString) throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
@Override
public void onDestroy() {
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.cancel();
}
}
@Override
protected void onStop() {
super.onStop();
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
}
快乐编码:)