我正在尝试在imageview上显示facebook和google plus个人资料图片。
为此我尝试了一些像......
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
user_picture.setImageBitmap(mIcon1);
这项工作在较低版本中很有用,但在较高版本中却没有。
我需要为更高版本做些什么。
更新
首先,我为同一个启动了一个AsyncTask ..有点像..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//new RegistrationAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,(String[]) null);
new DownloadImageTask(user_picture).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
} else{
new DownloadImageTask(user_picture).execute("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
}
这是我的AsyncTask类..
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
// private ProgressDialog mDialog;
private ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected void onPreExecute() {
// mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected Bitmap doInBackground(String... urls) {
Log.d("image", "do in");
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
Log.d("image", "do 1");
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
Log.d("image", "do 2");
} catch (Exception e) {
Log.e("Error", "image download error");
Log.e("Error", e.getMessage());
e.printStackTrace();
Log.d("image", "do catch");
}
Log.d("image", "do out");
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
//set image of your imageview
Log.d("image", "post");
bmImage.setImageBitmap(result);
//close
//mDialog.dismiss();
}
}
现在它再次像低版本的魅力一样工作,但在更高版本的android中却没有。
答案 0 :(得分:0)
您应该在后台线程中获取位图,因为在较高版本中不允许使用 NetworkOnUiThread 。所以它会抛出 NetworkOnUiThread Exception 。我正在 ICS 上发布完整的工作代码,并在清单中提供 INTERNET 权限。
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
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 {
private ImageView user_picture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_picture=(ImageView)findViewById(R.id.user);
new ImageTask().execute();
}
private class ImageTask extends AsyncTask<Void, Integer, Void> {
Bitmap mIcon1;
@Override
protected Void doInBackground(Void... params) {
try {
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+ userID +"/picture?type=square");
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if(mIcon1!=null)
{
user_picture.setImageBitmap(mIcon1);
}
}
}
}