根据Twitter文档
使用Fabric初始化TweetComposer Kit后,使用Builder开始构建Tweet编辑器。
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 scheme) 到本地文件。例如,
File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);
问题是,当共享对话框出现时,会出现一条带有“无法加载图像”消息的Toast。这是我的代码,我用Volley下载图像,然后在本地保存图像。检查文件是否存在是否成功,但之后图像未插入共享对话框中:
更新正如Alexander所说,解决方案是使用文件提供程序。这是更新的代码:
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>
答案 0 :(得分:1)
您可以使用文件提供程序来存储您要附加到Tweet的应用图片,从而解决此问题。此外,它是存储内部应用程序文件的最佳解决方案。 有关FileProvider的更多详细信息,请参阅此处:https://developer.android.com/reference/android/support/v4/content/FileProvider.html