简体   繁体   中英

FacebookException : Failed to copy image

I am getting this error while trying to use the Facebook SDK for Android.

This is my execute call:

new getBitmapFromUrl().execute("https://fs02.androidpit.info/userfiles/4110382/image/Android/android-6-0-marshmallow-hero-w782.jpg");

My async code:

public class getBitmapFromUrl extends AsyncTask<String,String,Bitmap>{
    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        URL url = null;
        try {
            url = new URL(params[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            InputStream inputStream = con.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        SharePhoto sharePhoto = new SharePhoto.Builder()
                .setBitmap(bitmap)
                .build();
        SharePhotoContent sharePhotoContent = new SharePhotoContent.Builder()
                .addPhoto(sharePhoto)
                .build();
 //shareButton is the facebookSdk default share button to call shareDialog
        shareButton.setShareContent(sharePhotoContent);
    }
}

This is my callback method:

shareButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
    @Override
    public void onSuccess(Sharer.Result result) {
        Log.e("Status", result.toString());
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException error) {
        Log.e("Status", error.toString());
    }
});

And my onActivity callback:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

My Manifest code:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <provider
        android:authorities="com.facebook.app.FacebookContentProvider1507510099556071"
        android:name="com.facebook.FacebookContentProvider"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.FacebookActivity"/>
</application>

If my code isn't correct, please suggest a good example. :-)

just change the exported attribute to true and it will work

<provider android:authorities="com.facebook.app.NativeAppCallContentProvider355198514515820"
              android:name="com.facebook.NativeAppCallContentProvider"
              android:exported="true"/>

Your code is fine. But need some minor tweaks.

Hope you have not added the correct bitmap for sharing. Check below things in your code.

In Post execute you added "mBitmap". You need to add "bitmap" which you received from the params.

On your onCreate kindly call and initiate the Facebook SDK and share dialog. Check below sample.

// Declaration

CallbackManager callbackManager;
ShareDialog shareDialog;

// Below should go under oncreate.

    FacebookSdk.sdkInitialize(this);
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);
    shareButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
// Initiate share by downloading the image.
        new getBitmapFromUrl().execute("https://fs02.androidpit.info/userfiles/4110382/image/Android/android-6-0-marshmallow-hero-w782.jpg");
      }
    });

// Download file from the path and return the Bitmap.

public class getBitmapFromUrl extends AsyncTask<String, String, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
  Bitmap bitmap = null;
  URL url = null;
  try {
    url = new URL(params[0]);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setDoInput(true);
    InputStream inputStream = con.getInputStream();
    bitmap = BitmapFactory.decodeStream(inputStream);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return bitmap;
}
    @Override
    protected void onPostExecute(Bitmap bitmap) {
      super.onPostExecute(bitmap);

      SharePhoto sharePhoto = new SharePhoto.Builder()
              .setBitmap(bitmap)
              .build();
      SharePhotoContent sharePhotoContent = new SharePhotoContent.Builder()
              .addPhoto(sharePhoto)
              .build();
//      shareButton.setShareContent(sharePhotoContent);
      shareDialog.show(sharePhotoContent);
    }
  }

Queries let me know.!

all above you have to done.

then the most important is that you have to make the file path like : "content://media/external/images/media/159745" to let the com.facebook.FacebookContentProvider can resolve this media.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM