【问题标题】:Share image on Twitter with TweetComposer?使用 TweetComposer 在 Twitter 上分享图片?
【发布时间】:2016-06-15 05:14:26
【问题描述】:

根据 Twitter 文档

使用 Fabric 初始化 TweetComposer Kit 后,使用 Builder 开始构建 Tweet Composer。

import com.twitter.sdk.android.tweetcomposer.TweetComposer;
...

TweetComposer.Builder builder = new TweetComposer.Builder(this)
     .text("just setting up my Fabric.")
     .image(myImageUri);
builder.show();

图像 Uri 应该是文件 Uri(即 file://absolute_path 方案) 到本地文件。例如,

File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);

问题在于,当共享对话框出现时,会出现一条带有“无法加载图像”消息的 toast。这是我的代码,我在其中使用 Volley 下载图像,然后将图像保存在本地。检查文件是否存在成功但之后图像没有插入到共享对话框中:

更新 正如 Alexander 建议的那样,解决方案是使用 File Provider。这是更新的代码:

public void showShareDialogTwitter(JSONObject response) {

    Gson gson = new GsonBuilder().create();
    final Advertisement item = gson.fromJson(response.toString(), Advertisement.class);

    // ==========================================================================

    ImageRequest request = new ImageRequest(item.getImages().get(0), new Response.Listener<Bitmap>() {

        public void onResponse(Bitmap response) {

            String path = giveThePathToImage(response);

            if (path != null && path != "") {

                //File myImageFile = new File(path + "/share.jpg");

                File file = new File(getFilesDir(), "share.jpg");

                if (file.exists()) {

                    Uri contentUri = FileProvider.getUriForFile(MainActivity.this,
                            "bg.web3.takeforfree.MainActivity", file);

                    //Uri myImageUri = Uri.fromFile(myImageFile);

                    String name;

                    if (item.getName() != null) {
                        name = item.getName();
                    } else {
                        name = "";
                    }

                    TweetComposer.Builder builder = new TweetComposer.Builder(MainActivity.this)
                            .text(MainActivity.this.getResources().getString(R.string.seeMyAdd) + name).image(contentUri);
                    builder.show();

                }
            }
        }
    }, 400, 400, null, null, new Response.ErrorListener() {

        public void onErrorResponse(VolleyError error) {

            Log.i("Sharing", "Error");

        }
    });


    Volley.newRequestQueue(this).add(request);

    // ==========================================================================
}

private String giveThePathToImage(Bitmap bitmapImage) {

    File directory = getFilesDir();

    File mypath = new File(directory, "share.jpg");

    FileOutputStream fos = null;

    try {

        fos = new FileOutputStream(mypath);

        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return directory.getAbsolutePath();
}

AndroidManifest.xml

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="bg.web3.takeforfree.MainActivity"
            android:exported="false"
            android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
        </provider>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="."/>
</paths>

【问题讨论】:

    标签: android twitter social-networking


    【解决方案1】:

    您可以通过使用 File Provider 来存储您想要附加到 Tweet 的应用图像来解决此问题。此外,它是存储内部应用程序文件的最佳解决方案。 有关 FileProvider 的更多详细信息,您可以在此处阅读:https://developer.android.com/reference/android/support/v4/content/FileProvider.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-27
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多