我想在 android 中使用 twitter4j 在 twitter 上上传图片
I want to upload image on twitter using twitter4j in android
使用 twitter4j 在 Twitter 上上传图片时,它适用于除 Lollipop 版本之外的所有手机。
你能帮帮我吗,我正在使用 AsyncTask
ConfigurationBuilder configurationbuilder = new ConfigurationBuilder();
configurationbuilder.setDebugEnabled(true);
configurationbuilder.setOAuthConsumerKey(twitter_consumer_key);
configurationbuilder.setOAuthConsumerSecret(twitter_secret_key);
configurationbuilder.setOAuthAccessToken(token);
configurationbuilder.setOAuthAccessTokenSecret(secret);
configurationbuilder.setMediaProviderAPIKey(twitpic_api_key);
twitter4j.conf.Configuration configuration = configurationbuilder
.build();
Twitter twitter = new TwitterFactory(configuration).getInstance();
// Log.d("ImageUploader", "Start sending image...");
try {
StatusUpdate status = new StatusUpdate(editTweet.getText()
.toString());
status.setMedia(new File(mPath));
twitter.updateStatus(status);
// url = imageupload.upload(new File(mPath));
//result = 1;
//System.out.println("URL Is : " + url);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getStatusCode();
e.getCause();
e.getMessage();
}
但当我将其用于棒棒糖版本时,它没有在推特上上传任何图像
对于 API >= 19 使用此代码
private String getRealPathFromURI_API19(Context context,
Uri mImageCaptureUri2) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(mImageCaptureUri2);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
对于 API < 19 使用此代码
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null,
null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
使用 twitter4j 在 Twitter 上上传图片时,它适用于除 Lollipop 版本之外的所有手机。 你能帮帮我吗,我正在使用 AsyncTask
ConfigurationBuilder configurationbuilder = new ConfigurationBuilder();
configurationbuilder.setDebugEnabled(true);
configurationbuilder.setOAuthConsumerKey(twitter_consumer_key);
configurationbuilder.setOAuthConsumerSecret(twitter_secret_key);
configurationbuilder.setOAuthAccessToken(token);
configurationbuilder.setOAuthAccessTokenSecret(secret);
configurationbuilder.setMediaProviderAPIKey(twitpic_api_key);
twitter4j.conf.Configuration configuration = configurationbuilder
.build();
Twitter twitter = new TwitterFactory(configuration).getInstance();
// Log.d("ImageUploader", "Start sending image...");
try {
StatusUpdate status = new StatusUpdate(editTweet.getText()
.toString());
status.setMedia(new File(mPath));
twitter.updateStatus(status);
// url = imageupload.upload(new File(mPath));
//result = 1;
//System.out.println("URL Is : " + url);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getStatusCode();
e.getCause();
e.getMessage();
}
但当我将其用于棒棒糖版本时,它没有在推特上上传任何图像
对于 API >= 19 使用此代码
private String getRealPathFromURI_API19(Context context,
Uri mImageCaptureUri2) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(mImageCaptureUri2);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
对于 API < 19 使用此代码
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null,
null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}