使用 TweetComposer 在 Twitter 上分享图片?

Share image on Twitter with TweetComposer?

根据 Twitter 文档

After initializing the TweetComposer Kit with Fabric, start construction of a Tweet composer by using the Builder.

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

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

The image Uri should be a file Uri (i.e. file://absolute_path scheme) to a local file. For example,

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

问题是,当共享对话框出现时,有一条消息 "The image cannot be loaded"。这是我的代码,我用 Volley 下载图像,然后在本地保存图像。检查文件是否存在成功,但在共享对话框中没有插入图像:

UPDATE 正如 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>

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