Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中从gallery上传Facebook中的照片?_Android_Facebook - Fatal编程技术网

如何在Android中从gallery上传Facebook中的照片?

如何在Android中从gallery上传Facebook中的照片?,android,facebook,Android,Facebook,我有这门课: public class FacebookShare extends Activity implements DialogListener { Facebook facebook = new Facebook("199622693386109"); Bitmap bitmap; byte [] data; @Override public void onCreate(Bundle savedInstanceState) {

我有这门课:

public class FacebookShare extends Activity implements DialogListener {
    Facebook facebook = new Facebook("199622693386109");
    Bitmap bitmap;
    byte [] data;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    
        Bundle parameters = new Bundle();
        parameters.putString("message", "Test Photo");
        parameters.putByteArray("picture", data);
        facebook.dialog(this, "stream.publish", parameters, this);
        facebook.authorize(this, new DialogListener()
        {
            public void onComplete(Bundle values) {}
            public void onFacebookError(FacebookError error) {}
            public void onError(DialogError e) {}
            public void onCancel() {}

        });
    }
}
但如果我运行此代码,我的应用程序就会崩溃。请帮忙

parameters.putByteArray("picture", data);
数据是图像的字节数组


下面是我在这个问题上的答案。…

我们可以使用带有多部分的Facebook graph API上传照片。 我正在使用翻新库进行网络呼叫

public interface ApiInterface {
    @Multipart
    @POST("/{id}/photos")
    Call<UserModelResponse> uploadPhoto(@Part MultipartBody.Part image, @Path("id")String pageId, @Query("access_token") String token);
}
可以上载多个图像

public class ApiCall {


private ApiInterface apiService;
private Context context;

public ApiCall(Context context) {
    this.context = context;
}

public ApiInterface getRetroFitService() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    Gson gson = new GsonBuilder().create();

    Retrofit builder = new Retrofit.Builder()
            .baseUrl("https://graph.facebook.com")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    apiService = builder.create(ApiInterface.class);
    return apiService;
}

public void uploadImageToFBPage(String pageId, ArrayList<Parcelable> path) throws IOException {

    for (Parcelable parcelable : path) {
        MultipartBody.Part body = prepareFilePart("source", (Uri) parcelable);
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);
        String token = sharedPreferences.getString("token", "");
        Call<UserModelResponse> call = apiService.uploadPhoto(body, pageId, token);
        call.enqueue(new Callback<UserModelResponse>() {
            @Override
            public void onResponse
                    (Call<UserModelResponse> call, Response<UserModelResponse> response) {
                if (response.errorBody() == null) {
                    Toast.makeText(context, "Image upload Success", Toast.LENGTH_SHORT).show();
                } else {
                    onFailure(call, new Exception());
                }
            }

            @Override
            public void onFailure(Call<UserModelResponse> call, Throwable t) {
                Toast.makeText(context, "Image upload Fail", Toast.LENGTH_SHORT).show();
            }
        });
    }
}


@NonNull
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
    // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
    // use the FileUtils to get the actual file by uri
    File file = FileUtils.getFile(context, fileUri);

    // create RequestBody instance from file
    RequestBody requestFile = RequestBody.create(MediaType.parse(context.getContentResolver().getType(fileUri)), file);

    // MultipartBody.Part is used to send also the actual file name
    return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);

}
 }

ndroid.app.ActivityThread.mainActivityThread.java:4363 04-27 15:05:49.716:ERROR/AndroidRuntime2595:at java.lang.reflect.Method.InvokEnable激活方法04-27 15:05:49.716:ERROR/AndroidRuntime2595:at java.lang.reflect.Method.invokeMethod:521 04-27 15:05:49.716:ERROR/AndroidRuntime2595:atcom.android.internal.os.ZygoteInit$methodAndArgscaler.runZygoteInit.java:860您是否正在使用facebook API..?是的,我已通过实时图像url在facebook中发送照片,但我必须从gellery上传照片。我已尝试但未成功。