我在我的Android应用程序中包含了Twitter Fabric SDK,可以发布推文。我也希望发布图片,但通过用于发布纯文本推文的StatusesService
似乎无法实现。
是不是可以通过SDK完成此操作?我是否必须为此做一个明确的http帖子?
答案 0 :(得分:2)
根据Fabric SDK,是的,您可以通过TweetUI在您的推文中嵌入媒体。
示例代码
使用Fabric初始化
str
推文代码
Fabric.with(this, new TweetComposer());
<强>记住强>
如果未安装Twitter应用程序,TweetComposer将会 创建在浏览器中与Twitter.com交互的意图。该 浏览器忽略指定的图像。
答案 1 :(得分:0)
package ...
public class StatusFragment extends Fragment {
Button btn_select, btn_post_image;
EditText edt_status;
String status;
ImageView img_status;
private static final int RESULT_LOAD_IMAGE = 1;
SharedPreferences mSharedPreferences;
String selectedImagePath;
Uri selectedImage;
TypedFile typedFile;
private static final String IMAGE_DIRECTORY_NAME = "Hello Twitter";
String mCurrentPhotoPath;
File mFile;
ProgressDialog pDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
btn_post_image = (Button) rootView.findViewById(R.id.btn_post_image);
btn_select = (Button) rootView.findViewById(R.id.btn_select);
edt_status = (EditText) rootView.findViewById(R.id.edt_status);
img_status = (ImageView) rootView.findViewById(R.id.img_status);
btn_post_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (StatusFragment.CheckNetwork.isInternetAvailable(getActivity())) //if connection available
{
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Uploading......");
pDialog.show();
final TwitterSession session = Twitter.getSessionManager().getActiveSession();
mFile = new File(getRealPathFromURI(selectedImage));
TypedFile typedFile = new TypedFile("application/octet-stream", mFile);
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(session);
MediaService ms = twitterApiClient.getMediaService();
ms.upload(typedFile, null, null, new Callback<Media>() {
@Override
public void success(Result<Media> mediaResult) {
// show on User Timeline
// StatusesService statusesService = TwitterCore.getInstance().getApiClient(session).getStatusesService();
// statusesService.update(edt_status.getText().toString(), null, false, null, null, null, true, false, mediaResult.data.mediaIdString, new Callback<Tweet>() {
// @Override
// public void success(Result<Tweet> tweetResult) {
// //...
// Toast.makeText(getActivity(), "Upload Complete", Toast.LENGTH_SHORT).show();
// pDialog.dismiss();
// }
//
// @Override
// public void failure(TwitterException e) {
// //...
// Toast.makeText(getActivity(), "Upload Error" + e.getMessage(), Toast.LENGTH_SHORT).show();
// pDialog.dismiss();
// }
//
// });
// Show on Home Timeline
StatusesService statusesService = TwitterCore.getInstance().getApiClient(session).getStatusesService();
statusesService.update( " content: " + edt_status.getText().toString(), null, false, null, null, null, true, false, mediaResult.data.mediaIdString, new Callback<Tweet>() {
@Override
public void success(Result<Tweet> tweetResult) {
pDialog.dismiss();
Toast.makeText(getActivity(), "Upload Completed", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(TwitterException e) {
//...
pDialog.dismiss();
Toast.makeText(getActivity(), "Upload Error"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void failure(TwitterException e) {
//...
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else{
Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show();
}
}
});
btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK) {
selectedImage = data.getData();
img_status.setImageURI(selectedImage);
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri contentUri) {
try {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return contentUri.getPath();
}
}
public static class CheckNetwork {
static String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null) {
Log.d(TAG, "no internet connection");
return false;
} else {
if (info.isConnected()) {
Log.d(TAG, " internet connection available...");
return true;
} else {
Log.d(TAG, " internet connection");
return true;
}
}
}
}
public class MyTwitterApiClient extends TwitterApiClient {
public MyTwitterApiClient(TwitterSession session) {
super(session);
}
public UploadMediaService getUploadMediaService() {
return getService(UploadMediaService.class);
}
}
interface UploadMediaService {
@Multipart
@POST("1.1/media/upload.json")
void upload(@Part("media") TypedFile file, @Part("additional_owners") String owners, Callback<MediaEntity> cb);
}
public void openPath(Uri uri) {
InputStream is = null;
try {
is = getActivity().getContentResolver().openInputStream(uri);
//Convert your stream to data here
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
试试吧!