【发布时间】:2017-09-29 10:58:15
【问题描述】:
这就是我想要做的!
我正在截取屏幕截图并将其保存在我的手机中,然后拾取相同的 uri 并通过 facebook 集成将其上传到 facebook 以供分享。
我的代码不起作用,我可以打开 facebook 分享对话框,它会在那里显示我的屏幕截图。但是当我按下发布按钮时,Facebook 会显示一个祝酒词“您的照片将被上传。请在通知栏中查看进度。”
在通知栏中,我收到一条消息“Facebook 上传失败。您的照片无法上传。”
这是我的代码
private static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
Log.e("Screenshot", "taken successfully");
return b;
}
public void saveBitmap(Bitmap bitmap) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File LuckPercentage = new File(pictureStorage, "Luck Percentage");
if (!LuckPercentage.exists())
LuckPercentage.mkdirs();
File imagePath = new File(LuckPercentage
+ "/screenshot-" + shot + ".png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Log.e("Screenshot", "saved successfully");
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
openScreenshot(imagePath);
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
uri = Uri.fromFile(imageFile);
Log.e("path", ""+uri);
intent.setDataAndType(uri, "image/*");
//startActivity(intent);
}
然后,我正在调用一个对话框,我想在其中显示我的屏幕截图和 Facebook 分享按钮。
public void open() {
dialog = new Dialog(context);
// super(context,R.style.Theme_Dialog_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.final_calculation);
dialog.getWindow().getAttributes().windowAnimations = R.style.Theme_Dialog_Translucent;
screen = (ImageView) dialog.findViewById(R.id.imageView);
final ShareButton shareButton = (ShareButton)dialog.findViewById(R.id.fb_share_button);
SharePhoto photo = new SharePhoto.Builder().setImageUrl(uri).build();
content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareButton.setShareContent(content);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ShareDialog.canShow(SharePhotoContent.class)) {
shareDialog.show(content);
}
}
});
dialog.show();
}
这是我添加到清单中的内容
<manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />
<meta-data
android:name="com.facebook.sdk.ApplicationName"
android:value="@string/app_name" />
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider707840309410113"
android:exported="true" />
</application>
</manifest>
【问题讨论】:
-
您在 android 错误日志中得到什么响应?
-
这是我在警告部分得到的。虽然我添加了 AppEventsLogger.activateApp(this);在 onCreate 方法中。 W/com.facebook.appevents.internal.ActivityLifecycleTracker:意外的活动暂停,没有匹配的活动恢复。记录数据可能不正确。确保从应用程序的 onCreate 方法调用 activateApp
-
我也试过 AppEventsLogger.activateApp(this);在 onResume() 方法中。但我仍然无法上传图片。
-
这也是我从 FacebookCallback
() 方法的 onSuccess 方法 E/fb 成功获得的输出:com.facebook.share.Sharer$Result@fc9984a