安卓:使用Parse获取facebook/Twitter个人资料图片



嗨,我正在为我的android应用程序使用ParseLoginUi,因此用户可以使用facebook、twitter和本地登录选项登录我的应用程序。我希望我的用户能够添加/上传个人资料图片。现在,当有人使用facebook登录时,我会为该用户获取facebook访问令牌,并将其存储到我的解析数据中。然而,我不知道如何获取该用户的facebook个人资料图片,以及将其存储在我的解析数据库中的最佳方式是什么(我应该将url保存为数据库中的字符串吗?如果用户从他/她的设备上传图像怎么办)?有人能帮我解决这个问题吗。

要获得用户的Facebook个人资料图片,您必须使用Facebook Android SDK V4(最新版本)。使用Facebook Android SDK获取图像后,您只需将url存储为字符串,并将其与您的parseuser关联!要从用户设备获取图像,请使用带有startactivityforresult的intent。

确保你已经配置了Facebook Android SDK,否则下面的所有说明对你没有什么用处!

步骤1。使用Facebook SDK从Facebook获取个人资料图像正式建议这应该在通过Parse SDK登录Facebook用户之后完成。

GraphJSONObjectCallback mCallback = new GraphJSONObjectCallback()
{
            @Override
            public void onCompleted(JSONObject mData, GraphResponse mResponse) 
            {
                if(mResponse.getError() == null)
                {   
                    try
                    {
                       final JSONObject mPicture = mData.getJSONObject("picture");                  
                       final JSONObject mPictureData = mPicture.getJSONObject("data");                  
                       final boolean mSilhouette = mPictureData.getBoolean("is_silhouette");                
                      **//this is the URL to the image that you want**
                      final String mImageUrl = mPictureData.getString("url"); 
                    }
                    catch (JSONException e)
                    {
                       //JSON Error, DEBUG
                    }
                }
                else
                {
                     //Facebook GraphResponse error, DEBUG
                }
           }
}; 
Bundle mBundle = new Bundle();
mBundle.putString("fields", "picture");
GraphRequest mGetUserRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), mCallback);
mGetUserRequest.setParameters(mBundle);
//if running this on the MAIN THREAD then use .executeAsync()
mGetUserRequest.executeAndWait();

现在你应该有字符串mImageUrl,所以你应该有IMAGE。布尔值mSilhuoette可以让您知道是否正在使用默认的silhuoette。查看Facebook Android SDK文档了解更多信息。

步骤2:将url与用户关联

ParseUser mUser = ParseUser.getCurrentUser(); 
mUser.put("image", mImageUrl);
mUser.saveInBackground();

注意:您也可以自己下载图像并将其存储在ParseFile中。这取决于您打算如何在整个应用程序中使用此图像。

步骤3:如果用户从他/她的设备上传图像,您可以使用带有startactivity的intent来允许用户拾取图像。在onActivityResult中获取图像。将图像加载到位图中。一旦图像加载到位图中,然后将位图转换为parsefile,并将该parsefile与用户关联,就完成了!我建议使用Picasso,以及显示您与ParseUser关联的图像url。你可以通过谷歌搜索Android Picasso来找到更多关于Picasso的信息。

当您准备允许用户从设备中选择图像时,请使用:

Image mImagePickerIntent = new Intent(); 
mImagePickerIntent.setType("image/*");
mImagePickerIntent.setAction(Intent.ACTION_GET_CONTENT);
final Intent mMainIntent = Intent.createChooser(mImagePickerIntent, "Pick Image");
startActivityForResult(mMainIntent, 1); 

在onActivityResult中使用此

@Override
public void onActivityResult(int mRequestCode, int mResultCode, Intent mIntent) 
{
    super.onActivityResult(mRequestCode, mResultCode, mIntent);
    if(mResultCode == Activity.RESULT_OK)
    {
       final Uri mImageUri = mIntent.getData();
       //might be better to load bitmap with a Picasso Target  (try/catch)
       Bitmap mBitmap = Picasso.with(getActivity()).load(mImageUri).get(); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       mBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
       final byte[] mData = bos.toByteArray();
       mBitmap.recycle(); 
       final ParseFile mPhotoFile = new ParseFile("image.png", mData);
       mPhotoFile.saveInBackground();
       final ParseUser mUser = ParseUser.getCurrentUser();
       mUser.put("imageFile", mPhotoFile); 
       mUser.saveInBackground();     
     }
}

上面的代码不是最终代码!它被认为代表了一个工作快照,你必须做什么来实现你的目标!为了简洁起见,省略了一些内容,如null检查、try/catch块等。

您可以从facebookId创建配置文件图片uri。

public static Uri getFacebookProfilePic(String fbId,int height, int width) {
    Uri.Builder builder = new Uri.Builder().encodedPath(String.format("https://graph.facebook.com/%s/picture", fbId));
    builder.appendQueryParameter("height", String.valueOf(height));
    builder.appendQueryParameter("width", String.valueOf(width));
    builder.appendQueryParameter("migration_overrides", "{october_2012:true}");
    return Uri.parse(builder.toString());
}

最新更新