我无法使用
获取图片 Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/100001220051873/picture");
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
Toast.makeText(getActivity(),"null it is",Toast.LENGTH_SHORT).show();
getBitmapFromURL在哪里
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
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;
}
}
但是当我在下面使用时,一切正常
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
但我已经在那里看到Strict mode in android 2.2这个政策使用起来并不安全,不仅如此,它还会阻止我的主UI几秒钟。
我想首先知道,为什么我不能在不使用上述政策的情况下获取Facebook个人资料照片,是否有任何方法可以避免上述政策并且可以获取我的个人资料照片?在此先感谢:)
答案 0 :(得分:4)
以下是工作代码:
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.d("response","response"+object.toString());
Intent intent=new Intent(MainActivity.this, ProfilePictureActivity.class);
try {
intent.putExtra("pic_url",object.getJSONObject("picture").getJSONObject("data").getString("url"));
startActivity(intent);
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture.width(300)");
request.setParameters(parameters);
request.executeAsync();