简体   繁体   中英

android - dropbox api - how to find and upload a file

Im using the dropbox api, and I want to upload a file on dropbox. There will be a file in dropbox with the same name that needs to be overwritten.

I tried this code to find the file profiles.txt, and upload it, but I always get file not found.

// create the file
FileOutputStream file = openFileOutput("profiles.txt", MODE_WORLD_READABLE);
// download the contents from the online file into the newly created file
DropboxFileInfo info = mDBApi.getFile("/profiles.txt", null, file, null);
// close the file
file.flush();
file.close();

// read the file
FileInputStream fstream = openFileInput("profiles.txt");
InputStreamReader isr = new InputStreamReader(fstream);
char[] inputBuffer = new char[7];
isr.read(inputBuffer);
String readString = (new String(inputBuffer)).trim();
int id = Integer.parseInt(readString);

// record your id
SharedPreferences mySharedPreferences = getSharedPreferences("User_info_file", MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("TEXT_ID_KEY", readString);
editor.commit();

// create the new file with the new id
file = openFileOutput("profiles.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(file);
osw.write(String.format("%d", id+1));
osw.flush();
osw.close();

File file2 = new File("profiles.txt");
String ab_path = (Tab_User_InfoActivity.this).getCacheDir().getAbsolutePath() + File.separator + "profiles.txt";
FileInputStream inputStream = new FileInputStream(ab_path); // CRASH HERE
Entry newEntry = mDBApi.putFile("profiles.txt", inputStream, file2.length(), null, null);

But I keep getting the file not found exception. Can anyone tell me what I am doing wrong?

您实际上可能需要为应用程序指定缓存路径,以确保文件位于您可以访问的位置,即:基于此设置文件名:

context.getCacheDir().getAbsolutePath() + File.separator + "profiles.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM