编辑:我写了一个函数来传递 Authenticated Box Client 和 Bitmap Bytestream。还被告知 Box 上的根目录的 ID 为 0。但我仍然无法上传。
我需要做的是创建一个目录,如果没有的话,然后上传该目录中的文件。
private void doUpload(final BoxAndroidClient client, final ByteArrayInputStream bs) {
if(client.isAuthenticated())
{
System.out.println("Trying to upload: In upload function");
AsyncTask<Null, Integer, Null> task = new AsyncTask<Null, Integer, Null>() {
@Override
protected void onPostExecute(Null result) {
Toast.makeText(PagerActivity.this, "done uploading", Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
Toast.makeText(PagerActivity.this, "start uploading", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
@Override
protected Null doInBackground(Null... params) {
try {
BoxDefaultRequestObject folreqObj = new BoxDefaultRequestObject();
BoxFolder newFol = new BoxFolder();
String parentId = newFol.getId();
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject("0", "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
/*
if(client.getFoldersManager().getFolder(parentId, folreqObj).getName()!=null)
{
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
}
else
{
BoxFolderRequestObject upreqObj = BoxFolderRequestObject.createFolderRequestObject("BestPhotoNow", parentId);
client.getFoldersManager().createFolder(upreqObj);
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
}*/
} catch (BoxRestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BoxJSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BoxServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthFatalFailureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute();
}
}
所以,我有一个壁纸应用程序,可以从服务器获取一些图像。我正在尝试集成 box,以便能够通过 Box Android API v2 将当前视图(viewpager)中的图片上传到 Box。
https://github.com/box/box-android-sdk-v2
我已成功通过 oauth 对用户进行身份验证。但是,我真的坚持尝试将位图或 uri 直接上传到他们的上传调用,而无需启动文件夹选择器活动。这就是我所拥有的。
单击按钮进行上传:
case R.id.boxcloudbutton:
imageLoader.loadImage(url, new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Intent intent = OAuthActivity.createOAuthActivityIntent(getApplicationContext(), "vfoj4o6nxqblahh454wem", "J9TQWyFSblahV2C", false, "http://localhost");
intent.putExtra("boximageUri", imageUri);
System.out.println("BoxUri: "+imageUri);
startActivityForResult(intent, BOX_RESULT);
super.onLoadingComplete(imageUri, view, loadedImage);
}
});
break;
我正在尝试传递位图或 uri
并在 OnActivityResult
case BOX_RESULT:
if (resultCode == Activity.RESULT_CANCELED) {
// Get the error message for why authentication failed.
String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
// Implement your own logic to handle the error.
// handleFail(failMessage);
} else {
// You will get an authenticated oath token object back upon success.
BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
// If you don't want to supply a customized hub or parser, use null to fall back to defaults.
BoxAndroidClient client = new BoxAndroidClient( "vfoj4oxxxxxxxxxxtordlh454wem",
"J9TjxxxxxxxmRwux2V2C", null, null, null);
client.authenticate(oauth);
System.out.println("Data for Box: "+data.getExtras().getString("boximageUri"));
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(0, "name", file);
BoxFile bFile = client.getFilesManager().uploadFile(requestObj);
}
break;
在这里,我成功通过了身份验证,但我没有收到我传递的 Intent 数据中的 uri 或位图。
此外,如果有人知道如何将位图传递给文件以便为 Box api v2 上传文件,我将非常感谢您的帮助!
谢谢, 阿纳布