在tweet作曲家中添加图像

时间:2017-02-28 11:33:41

标签: android twitter twitter-fabric

我正在使用Fabric插件撰写一条共享文本和图像组合的推文。我想直接从链接共享图像,如下所示:

private void composetweet() {
        TweetComposer.Builder builder=new TweetComposer.Builder(this)
                .text("This is made by saptak das")
                .image(Uri.parse("http://i.dailymail.co.uk/i/pix/2015/09/21/16/2C96F60E00000578-0-image-a-126_1442848442984.jpg"));
        builder.show();
    }

问题是文本已成功解析到推文中,但是在图片的情况下,会出现一个吐司,说图片无法加载

1 个答案:

答案 0 :(得分:0)

您需要在设备上下载图片(我为此使用Glide),然后获取其uri并将其传递给TweetComposer。这是解决方案:

Glide.with(Context)
            .load(imageUri)
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                //calling when downloading finished
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                    //creating new file and getting its uri
                    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    file = new File(path + "/DemoImage.jpg");
                    if (!file.getParentFile().exists())
                        file.getParentFile().mkdirs();
                    try {

                        if (file.exists())
                            file.delete();
                        file.createNewFile();

                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    }catch (Exception e){
                        Log.e(TAG, e.getMessage());
                    }

                    if (file != null)
                        makeShare(Uri.fromFile(file));
                    else
                        makeShare(null);

                }
            });

这是makeShare(Uri uri)方法代码:

void makeShare(Uri imagePath){
    if (imagePath != null){
        TweetComposer.Builder builder = new TweetComposer.Builder(viewInterface.getContext())
                .text("Your message")
                .image(imagePath);
        builder.show();
    }else {
        TweetComposer.Builder builder = new TweetComposer.Builder(viewInterface.getContext())
                .text("Your message");
        builder.show();
    }
}

另请注意,您需要申请权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>