gpt4 book ai didi

java - 使用Facebook SDK 4在Android中加载Facebook个人资料图片时获取异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:28 24 4
gpt4 key购买 nike

我试图在导航抽屉顶部的我的Android应用程序中加载一个Facebook个人资料图片,但似乎出现了异常。我已经遍历了堆栈溢出,解释了如何使用位图,毕加索等来加载图像,但是由于某些原因仍无法加载。我认为该问题是某些网络线程问题,但是我认为因为我使用.executeAsync()执行该方法就没有关系了。
这是xml,我在其中声明要作为个人资料图片的图像(nav_header_main.xml):
    
    

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/hanger_circle"
app:border_color="#FF000000"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"/>



</RelativeLayout>


这是我要求提供图片的活动的摘录(在成功完成Facebook登录后:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//initialize facebook sdk
FacebookSdk.sdkInitialize(getApplicationContext());

setContentView(R.layout.activity_login);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);

//call custom application class to initialize intercom (not login, just setup)
ApplicationHelper helper = (ApplicationHelper)getApplicationContext();

//create facebook button recognition
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//facebook picture stuff
Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
if (response != null) {
try {
JSONObject data = response.getJSONObject();
if (data.has("picture")) {
String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
// set profile image to imageview using Picasso or Native methods
Bitmap profilePic = getFacebookProfilePicture(profilePicUrl);
ImageView mImageView = (ImageView) findViewById(R.id.profile_image);
mImageView.setImageBitmap(profilePic);
}
} catch (Exception e) {
e.printStackTrace();
}

}
}
}).executeAsync();



// launch next activity
Toast.makeText(getApplicationContext(), "SUCCESS", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();

}

@Override
public void onCancel() {
// App code
Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT).show();

}

@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_SHORT).show();

}
});


//check if facebook user is logged in already
AccessToken token = AccessToken.getCurrentAccessToken();
Profile prof = Profile.getCurrentProfile();
if(token != null && prof != null){
//log in
//Toast.makeText(getApplicationContext(), token.getToken(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
//check if regular user is logged in already
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
if(shared.contains("username") && shared.contains("password")){
//password exists in file so launch next activity (make sure there correct with API)
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}


}


我也使用这种辅助方法:

public static Bitmap getFacebookProfilePicture(String url){
URL facebookProfileURL= null;
Bitmap bitmap = null;
try {
facebookProfileURL = new URL(url);
bitmap = BitmapFactory.decodeStream(facebookProfileURL.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}


这是我的例外:
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:android.os.NetworkOnMainThreadException
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:at java.net.InetAddress.getAllByName(InetAddress.java:215)
05-26 14:20:05.751 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.HostResolver $ 1.getAllByName(HostResolver.java:29)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.press.press.LoginActivity.getFacebookProfilePicture(LoginActivity.java:215)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.press.press.LoginActivity $ 1 $ 1.onCompleted(LoginActivity.java:85)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.facebook.GraphRequest $ 5.run(GraphRequest.java:1379)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at android.os.Handler.handleCallback(Handler.java:739)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at android.os.Looper.loop(Looper.java:135)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5221)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at java.lang.reflect.Method.invoke(本机方法)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at java.lang.reflect.Method.invoke(Method.java:372)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
05-26 14:20:05.752 11101-11101 / com.android.press.press W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果我做错了事,请告诉我。我尝试了多种方法,但这似乎是最受欢迎的答案。我将非常感谢您的帮助。提前致谢

最佳答案

而不是使用此行中使用的输入流

bitmap = BitmapFactory.decodeStream(facebookProfileURL.openConnection().getInputStream());


您应该使用Picasso Library在ImageView中显示图像,并传递url(不启动任何连接,不使用输入流,而仅使用从Facebook接收的url):

Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);


this answer中所示。

关于java - 使用Facebook SDK 4在Android中加载Facebook个人资料图片时获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com