我正在尝试使用他们的API将图像上传到Twitter,但它始终无法抛出错误,说它无法找到该文件,即使它肯定存在,因为我使用相同的路径将其上传到其他网络,它在我的预览框中正确显示。
com.twitter.sdk.android.core.TwitterApiException: /-1/1/content:/media/external/images/media/100883/ORIGINAL/NONE/1159437588: open failed: ENOENT (No such file or directory)
W/System.err: at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:390)
W/System.err: at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
W/System.err: at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
W/System.err: at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
一些谷歌搜索显示这个错误本来是在Android的Twitter SDK的旧版本中修复的,人们已经确认它是,但那是不久前我正在使用最新版本{{1} }。
我用来上传的代码如下:
1.13.0
有谁知道这里出了什么问题以及我怎么能让这个工作?
答案 0 :(得分:0)
/**
* @return The MIME type for the given file.
*/
private String getMimeType(File file) {
final String ext = getExtension(file.getName());
if (!TextUtils.isEmpty(ext)) {
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
}
// default from https://dev.twitter.com/rest/public/uploading-media
return "application/octet-stream";
}
/**
* @return the extension of the given file name, excluding the dot. For example, "png", "jpg".
*/
private String getExtension(String filename) {
if (filename == null) {
return null;
}
final int i = filename.lastIndexOf(".");
return i < 0 ? "" : filename.substring(i + 1);
}
private void socialTwitterLoginAndUploadMedia(){
mTwitterAuthClient.authorize(this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Call<User> user = TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false, false);
user.enqueue(new Callback<User>() {
@Override
public void success(Result<User> userResult) {
tw_User_name = userResult.data.name;
tw_User_screen_Name = userResult.data.screenName;
tw_User_id = String.valueOf(userResult.data.id);
tw_User_profile_image = userResult.data.profileImageUrl;
if(imageFile != null) {
final TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
MyTwitterApiClient myTwitterApiClient = new MyTwitterApiClient(session);
RequestBody requestBodyprofilePic = null;
MultipartBody.Part body = null;
final String mimeType = getMimeType(imageFile);
requestBodyprofilePic = RequestBody.create(MediaType.parse(mimeType), imageFile);
// body = MultipartBody.Part.createFormData("media", imageFile.getName(), requestBodyprofilePic);
myTwitterApiClient.getMediaService().upload(requestBodyprofilePic, null, null).enqueue(new retrofit2.Callback<Media>() {
@Override
public void onResponse(Call<Media> call, Response<Media> response) {
Log.e("Upload Success Id int=", response.body().mediaId+"=");
Log.e("Upload Success Id=", response.body().mediaIdString);
Log.e("Upload Success Size=", response.body().size+"=");
Log.e("Upload Success ImageType=", response.body().image.imageType);
}
@Override
public void onFailure(Call<Media> call, Throwable t) {
Log.e(TAG, "Upload Tweet Error");
}
});
}
}
@Override
public void failure(TwitterException exception) {
// Upon error, show a toast message indicating that authorization request failed.
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void failure(TwitterException exception) {
// Upon error, show a toast message indicating that authorization request failed.
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}