我开始探索 Android 应用程序开发,并尝试将纯 txt 文件上传到保管箱以开始。每当我完成这项工作时,我都会升级到 pdf 等。
无论如何,我一直在上传到 Dropbox。我已经包含了这些库,在我的 AndroidManifest 中获得了活动,并尽可能地遵循官方指南。废话不多说,这是我的代码:
AndroidManifest.xml:
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboard">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-mykeyhere" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我触发onclick上传的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".HelloDropboxActivity" >
<Button
android:id="@+id/dropbox_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/link_button"/>
<TextView
android:id="@+id/test_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我的活动:
public class Settings extends Activity{
final static private String APP_KEY = "myAppKeyIsHere";
final static private String APP_SECRET = "myAppSecretIsHere";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
// In the class declaration section:
private DropboxAPI<AndroidAuthSession> mDBApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// And later in some initialization function:
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
}
public void dropbox_button(View v){
mDBApi.getSession().startAuthentication(Settings.this);
String filePath = getApplicationContext()
.getFilesDir()
.getPath()
.toString() + "/magnus-opus.txt";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog",
"The uploaded file's rev is: " + response.rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void onResume() {
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth,
// sets the access token on the session
mDBApi.getSession().finishAuthentication();
AccessTokenPair tokens = mDBApi
.getSession()
.getAccessTokenPair();
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
有人可以告诉我这里有什么问题或帮助我吗?现在我只想能够将一个 .txt 文件上传到我的保管箱,里面有任何东西。谢谢
~延特