使用 TweetComposer 在 Twitter 上分享图片? [英] Share image on Twitter with TweetComposer?

查看:37
本文介绍了使用 TweetComposer 在 Twitter 上分享图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据推特文档

<块引用>

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

import com.twitter.sdk.android.tweetcomposer.TweetComposer;...TweetComposer.Builder builder = new TweetComposer.Builder(this).text("只是设置我的面料.").image(myImageUri);builder.show();

<块引用>

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

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

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

更新 正如亚历山大所建议的,解决方案是使用文件提供程序.这是更新后的代码:

public void showShareDialogTwitter(JSONObject response) {Gson gson = new GsonBuilder().create();最终广告项目 = gson.fromJson(response.toString(), Advertisement.class);//==========================================================================ImageRequest request = new ImageRequest(item.getImages().get(0), new Response.Listener() {public void onResponse(位图响应){字符串路径 = giveThePathToImage(response);if (path != null && path != "") {//File myImageFile = new File(path + "/share.jpg");File file = new File(getFilesDir(), "share.jpg");如果(文件.存在()){Uri contentUri = FileProvider.getUriForFile(MainActivity.this,"bg.web3.takeforfree.MainActivity", 文件);//Uri myImageUri = Uri.fromFile(myImageFile);字符串名称;if (item.getName() != null) {name = item.getName();} 别的 {名称 = "";}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() {公共无效onErrorResponse(VolleyError错误){Log.i("分享", "错误");}});Volley.newRequestQueue(this).add(request);//==========================================================================}私人字符串 giveThePathToImage(Bitmap bitmapImage) {文件目录 = getFilesDir();File mypath = new File(directory, "share.jpg");FileOutputStream fos = null;尝试 {fos = new FileOutputStream(mypath);bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);} 捕获(异常 e){e.printStackTrace();} 最后 {尝试 {fos.close();} catch (IOException e) {e.printStackTrace();}}返回 directory.getAbsolutePath();}

AndroidManifest.xml

</提供者>

file_paths.xml

解决方案

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

According to Twitter documentation

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 problem is that when the share dialog shows up there is a toast with a message "The image cannot be loaded". Here is my code where I download the image with Volley and after that saving the image locally. The check whether the file exists is successful but after that the image is not inserted in the sharing dialog:

UPDATE As Alexander suggested, the solution is with using File Provider. Here is the updated code:

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>

解决方案

You can solve this problem by using File Provider to store your app images, which you want to attach to Tweet. Also, it is the best solution for storing your inner app files. More details about FileProvider you can read here: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

这篇关于使用 TweetComposer 在 Twitter 上分享图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆