Share image on Twitter with TweetComposer?

回眸只為那壹抹淺笑 提交于 2019-12-24 06:29:27

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/37826662/share-image-on-twitter-with-tweetcomposer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!