android共享应用程序

时间:2013-01-09 12:38:51

标签: android facebook photo sharing

我的Android应用程序有一些问题。我想与fb和twitter分享一些文字和照片。第一个问题是我无法将文本从我的文本字段复制到fb消息...我的意思是我有一个文本字段,其中包含您要共享的文本和2个按钮(fb,twitter)和twitter端正在工作很好,但我无法将文本附加到fb消息(按下按钮时窗口显示,但它是空白的)。这是我在facebbok上分享的代码:

Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    txt.getText());
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    txt.getText());
            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(
                    shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ((app.activityInfo.name).contains("facebook")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }

我的另一个问题是我的照片浏览器给了我这样一个文件的路径:“external / images / media / photo”我认为问题是我必须复制扩展名(.jpg等) 。)但不是这样。如果我使用直接路径:“/ mnt / sdcard / DCIM/01.jpg”它工作正常。有人可以帮助我找到照片的直接路径,或者我如何修改我的代码,它与第一个一起工作:

private void share(String nameApp, String imagePath, String text) {
    try {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager()
                .queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = new Intent(
                        android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpeg");
                if (info.activityInfo.packageName.toLowerCase().contains(
                        nameApp)
                        || info.activityInfo.name.toLowerCase().contains(
                                nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
                    targetedShare.putExtra(Intent.EXTRA_TEXT, text);
                    targetedShare.putExtra(Intent.EXTRA_STREAM,
                            Uri.fromFile(new File(imagePath)));
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(
                    targetedShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[] {}));
            startActivity(chooserIntent);
        }
    } catch (Exception e) {
    }
}

Thx预计......

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式获取图片的确切路径,我已经分享了照片&amp; FB&amp; Twitter你分享的方式和它的工作也很好。希望这会对你有所帮助。

<强>编辑:

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
        }
    }
}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}