我想上传图片和文件到DropBox,它工作正常,但当我从DropBox打开上传的图像和文件时,它没有打开。它显示打开失败。我怎么解决这个问题?我的代码如下所示:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private FragmentManager fragmentManager = null;
private FragmentTransaction fragmentTransaction = null;
private DirectoryFragment mDirectoryFragment;
private DropboxAPI<AndroidAuthSession> dropbox;
private final static String APP_KEY = "APP_KEY";
private final static String APP_SECRET = "APP_SECRET";
private static final int PICKFILE_REQUEST_CODE = 1;
private String Path = "";
private int COME_FROM = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
dropbox = new DropboxAPI<AndroidAuthSession>(session);
dropbox.getSession().startOAuth2Authentication(MainActivity.this);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle("Directory");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
mDirectoryFragment = new DirectoryFragment();
mDirectoryFragment.setDelegate(new DocumentSelectActivityDelegate() {
@Override
public void startDocumentSelectActivity() {
}
@Override
public void didSelectFiles(DirectoryFragment activity,
ArrayList<String> files) {
Log.d("Selected Files::::", files.get(0).toString());
Path = files.get(0).toString();
if (!Path.equals("")) {
Log.d("Path::::", Path);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(Path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DropboxAPI.Entry response = null;
try {
response = dropbox.putFile(Path, inputStream,
Path.length(), null, null);
} catch (DropboxException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, Path + " Upload file successfull",
Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(MainActivity.this, "Picked file failed",
Toast.LENGTH_LONG)
.show();
}
}
@Override
public void updateToolBarName(String name) {
toolbar.setTitle(name);
}
});
fragmentTransaction.add(R.id.fragment_container, mDirectoryFragment, "" + mDirectoryFragment.toString());
fragmentTransaction.commit();
}
@Override
protected void onDestroy() {
mDirectoryFragment.onFragmentDestroy();
super.onDestroy();
}
@Override
public void onBackPressed() {
if (mDirectoryFragment.onBackPressed_()) {
super.onBackPressed();
}
}
protected void onResume() {
super.onResume();
if (dropbox.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
dropbox.getSession().finishAuthentication();
String accessToken = dropbox.getSession().getOAuth2AccessToken();
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
}
答案 0 :(得分:0)
response = dropbox.putFile(Path, inputStream,
Path.length(), null, null);
第三个参数应该是文件的长度,而不是路径的长度。如果您检查上传文件的大小,我认为您发现它只有几个字节(无论长度是Path
)。
我觉得这样的事情可能有用:
response = dropbox.putFile(Path, inputStream,
new File(Path).length(), null, null);