ファブリックとTweetComposerキットを初期化した後、Twitterのドキュメントツイッターで画像を共有するTweetComposer?
によると、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();
画像ウリは、ウリ(すなわちファイル:// absolute_path方式)ファイルであるべきであるローカルファイルに 。例えば、
File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);
問題は、共有ダイアログが現れたときに「ロードできない画像」メッセージとトーストがあることです。ここで私がVolleyで画像をダウンロードした後、その画像をローカルに保存するコードがあります。ファイルが存在するかどうかのチェックは成功ですが、それ以降は画像が共有ダイアログに挿入されません。
UPDATEアレクサンダーが提案したように、解決策はファイルプロバイダを使用することです。
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>