我一直在尝试在stackoverflow上发布的许多代码。但到目前为止,没有一个对我有用。我是android的新手,可能有些东西我忽略了。请帮助修复此代码 -
package com.alphageeks.pespitstop;
import java.io.InputStream;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Vop extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vop);
Button bnot = (Button) findViewById(R.id.bnot);
Button bcant = (Button) findViewById(R.id.bcant);
Button bcal = (Button) findViewById(R.id.bcal);
final ImageView img = (ImageView) findViewById(R.id.imageView1);
final Toast disp = null;
bnot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
URL url = new URL("https://drive.google.com/file/d/0B-bEff6i-vWoUHpWS3h1VUR4ZkE/edit?usp=sharing");
//try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (Exception ex) {
disp.makeText(Vop.this, "error",Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:3)
两件事:
1)您想要的网址实际上是:
2)您需要在后台Thread
下载图像,就像AsyncTask
一样,否则您将抛出NetworkOnMainThreadException
。< / p>
private class ImageWorker extends AsyncTask<String, Void, Bitmap> {
/**
* {@inheritDoc}
*/
@Override
protected Bitmap doInBackground(String... params) {
InputStream input = null;
try {
final URL url = new URL(YOUR_URL);
input = new BufferedInputStream(url.openStream());
return BitmapFactory.decodeStream(input);
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (final IOException ignored) {
// Nothing to do
}
}
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
// Set the image here
}
}
}
答案 1 :(得分:1)
试试这个(如果你没有得到任何例外,这是以防万一,否则你可能在UI线程上做网络并导致错误,那么你可能会忽略我的回复):
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
byte[] bytes = out.toByteArray();
Bitmap bmp=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
img.setImageBitmap(bmp);
答案 2 :(得分:0)
也许你可以看看这个教程。 我希望它可以解决你的问题。
https://sites.google.com/site/androidhowto/diplayinganimage
或者这个网站上有关于如何从URL显示图像的更详细的教程。 不要忘记在清单中包含权限。
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
答案 3 :(得分:0)
使用picasso http://square.github.io/picasso/很简单,代码示例Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
答案 4 :(得分:0)
试试这个:
try {
URL url = new URL("https://drive.google.com/file/d/0B-bEff6i-vWoUHpWS3h1VUR4ZkE/edit?usp=sharing");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
答案 5 :(得分:0)
试试这个......可能对你有帮助..
URL url = new URL("YourUrl");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);