无法从android向dropbox发送文本文件
我尝试了很多发送文本文件到下拉框,但它不工作。据我所知,我已经正确地完成了所有的编码。有人请给我你的电子邮件id或回复我Lalit12131@gmail.com 我会把我所有的文件和细节发给你,让我来解决这个问题 请帮忙 提前谢谢无法从android向dropbox发送文本文件,android,dropbox,Android,Dropbox,我尝试了很多发送文本文件到下拉框,但它不工作。据我所知,我已经正确地完成了所有的编码。有人请给我你的电子邮件id或回复我Lalit12131@gmail.com 我会把我所有的文件和细节发给你,让我来解决这个问题 请帮忙 提前谢谢 void toDropbox() { b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {
void toDropbox()
{
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String filePath =getApplicationContext().getFilesDir().getPath().toString() + "/DropboxFile1.txt";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "In clickListener", Toast.LENGTH_SHORT).show();
try {
DropboxAPI.Entry response = mDBApi.putFile("/DropboxFile1.txt", inputStream, file.length(), null, null);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用此代码,您就可以开始了,删除一些错误,因为我使用此代码上载图像、文本文件和pdf文件。我删除了很多代码
public class Dropboxupload extends Activity {
private static final String TAG = "Dropbox";
///////////////////////////////////////////////////////////////////////////
// Your app-specific settings. //
///////////////////////////////////////////////////////////////////////////
// Replace this with your app key and secret assigned by Dropbox.
// Note that this is a really insecure way to do this, and you shouldn't
// ship code which contains your key & secret in such an obvious way.
// Obfuscation is good.
final static private String APP_KEY = "xxxxxxxxx";
final static private String APP_SECRET = "xxxxxxxxx";
// If you'd like to change the access type to the full Dropbox instead of
// an app folder, change this value.
final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
///////////////////////////////////////////////////////////////////////////
// End app-specific settings. //
///////////////////////////////////////////////////////////////////////////
// You don't need to change these, leave them alone.
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
DropboxAPI<AndroidAuthSession> mApi;
private boolean mLoggedIn;
// Android widgets
private Button mSubmit;
private LinearLayout mDisplay;
private Button mPhoto;
private Button mRoulette;
String filename = Scene.name;
private ImageView mImage;
private final String PHOTO_DIR = "/Your directory here/";
final static private int NEW_PICTURE = 1;
private String mCameraFileName;
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.forsettings);
TextView actiontitle=(TextView)findViewById(R.id.textscene);
actiontitle.setText("Upload");
Button homebutton = (Button)findViewById(R.id.home);
homebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
view.getContext().startActivity(new Intent(view.getContext().getApplicationContext(),Main.class));
}
});
if (savedInstanceState != null) {
mCameraFileName = savedInstanceState.getString("mCameraFileName");
}
// We create a new AuthSession so that we can use the Dropbox API.
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
// Basic Android widgets
setContentView(R.layout.activity_dropboxupload);
checkAppKeySetup();
mSubmit = (Button)findViewById(R.id.auth_button);
mSubmit.setBackgroundColor(Color.rgb(175, 246, 179));
Button mEmail = (Button)findViewById(R.id.email_button);
mEmail.setBackgroundColor(Color.rgb(175, 246, 179));
mSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// This logs you out if you're logged in, or vice versa
if (mLoggedIn) {
logOut();
} else {
// Start the remote authentication
mApi.getSession().startAuthentication(Dropboxupload.this);
}
}
});
mSubmit.setVisibility(View.INVISIBLE);
mDisplay = (LinearLayout)findViewById(R.id.logged_in_display);
// This is where a photo is displayed
mImage = (ImageView)findViewById(R.id.image_view);
// Display the proper UI state if logged in or not
setLoggedIn(mApi.getSession().isLinked());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("mCameraFileName", mCameraFileName);
super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
AndroidAuthSession session = mApi.getSession();
// The next part must be inserted in the onResume() method of the
// activity from which session.startAuthentication() was called, so
// that Dropbox authentication completes properly.
if (session.authenticationSuccessful()) {
try {
// Mandatory call to complete the auth
session.finishAuthentication();
// Store it locally in our app for later use
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(true);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
Log.i(TAG, "Error authenticating", e);
}
}
}
// This is what gets called on finishing a media piece to import
public void uploadData() {
String filename = Scene.name;
filename.replaceAll(".txt", "");
filename.replaceAll(".pdf", "");
filename.replaceAll(".fdx", "");
db.getContact(filename);
Contact cn = db.getContact(filename);
String text = cn.getscript();
//// get file path from here
String filePath = getApplicationContext().getFilesDir().getPath().toString() + filename + ".txt";
File file = new File(filePath);
file.canWrite();
FileWriter writer = null;
try {
writer = new FileWriter(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
writer.append(text);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UploadPicture upload = new UploadPicture(this, mApi, PHOTO_DIR, file);
upload.execute();
//v.getContext().startActivity(new Intent(v.getContext().getApplicationContext(),Scene.class));
}
private void logOut() {
// Remove credentials from the session
mApi.getSession().unlink();
// Clear our stored keys
clearKeys();
// Change UI state to display logged out version
setLoggedIn(false);
}
/**
* Convenience function to change UI state based on being logged in
*/
private void setLoggedIn(boolean loggedIn) {
mLoggedIn = loggedIn;
if (loggedIn) {
mSubmit.setText("Unlink from Dropbox");
mSubmit.setVisibility(View.INVISIBLE);
// mDisplay.setVisibility(View.VISIBLE);
} else {
mSubmit.setText("Link with Dropbox");
mDisplay.setVisibility(View.GONE);
mImage.setImageDrawable(null);
}
}
////////////////////
//////// Do not look at functions under this
///////////////////
private void checkAppKeySetup() {
// Check to make sure that we have a valid app key
if (APP_KEY.startsWith("CHANGE") ||
APP_SECRET.startsWith("CHANGE")) {
showToast("You must apply for an app key and secret from developers.dropbox.com, and add them to the Dropbox ap before trying it.");
finish();
return;
}
// Check if the app has set up its manifest properly.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
String scheme = "db-" + APP_KEY;
String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
testIntent.setData(Uri.parse(uri));
PackageManager pm = getPackageManager();
if (0 == pm.queryIntentActivities(testIntent, 0).size()) {
showToast("URL scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
finish();
}
}
private void showToast(String msg) {
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
/**
* Shows keeping the access keys returned from Trusted Authenticator in a local
* store, rather than storing user name & password, and re-authenticating each
* time (which is not to be done, ever).
*
* @return Array of [access_key, access_secret], or null if none stored
*/
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(ACCESS_KEY_NAME, null);
String secret = prefs.getString(ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
/**
* Shows keeping the access keys returned from Trusted Authenticator in a local
* store, rather than storing user name & password, and re-authenticating each
* time (which is not to be done, ever).
*/
private void storeKeys(String key, String secret) {
// Save the access key for later
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(ACCESS_KEY_NAME, key);
edit.putString(ACCESS_SECRET_NAME, secret);
edit.commit();
}
private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
}
return session;
}
}
公共类Dropboxupload扩展活动{
私有静态最终字符串TAG=“Dropbox”;
///////////////////////////////////////////////////////////////////////////
//您的应用程序特定设置//
///////////////////////////////////////////////////////////////////////////
//将此替换为Dropbox分配的应用程序密钥和密码。
//请注意,这是一种非常不安全的方法,您不应该这样做
//以如此明显的方式发送包含您的密钥和机密的代码。
//混淆是好的。
最终静态私有字符串APP_KEY=“xxxxxxxxx”;
最终静态私有字符串APP_SECRET=“xxxxxxxxx”;
//如果要将访问类型更改为完整的Dropbox而不是
//在应用程序文件夹中,更改此值。
最终静态私有AccessType ACCESS\u TYPE=AccessType.DROPBOX;
///////////////////////////////////////////////////////////////////////////
//结束特定于应用程序的设置//
///////////////////////////////////////////////////////////////////////////
//你不需要改变这些,别管它们。
最终静态私有字符串帐户\u PREFS\u NAME=“PREFS”;
最终静态私有字符串访问密钥\u NAME=“访问密钥”;
最终静态私有字符串访问\u SECRET\u NAME=“访问\u SECRET”;
DropboxAPI mApi;
私有布尔型mLoggedIn;
//Android小部件
私有按钮mSubmit;
私人线路布局mDisplay;
私人按钮mPhoto;
私人按钮;
字符串文件名=Scene.name;
私有图像视图;
私有最终字符串PHOTO_DIR=“/Your directory here/”;
最终静态私有int新图片=1;
私有字符串mCameraFileName;
DatabaseHandler db=新的DatabaseHandler(此);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ActionBar ActionBar=getActionBar();
actionBar.setBackgroundDrawable(新彩色Drawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.forsettings);
TextView actiontitle=(TextView)findViewById(R.id.textscene);
actiontitle.setText(“上传”);
按钮主页按钮=(按钮)findViewById(R.id.home);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图){
//TODO自动生成的方法存根
view.getContext().startActivity(新意图(view.getContext().getApplicationContext(),Main.class));
}
});
如果(savedInstanceState!=null){
mCameraFileName=savedInstanceState.getString(“mCameraFileName”);
}
//我们创建了一个新的AuthSession,以便可以使用Dropbox API。
AndroidAuthSession会话=buildSession();
mApi=新的DropboxAPI(会话);
//基本Android小部件
setContentView(R.layout.activity_dropboxupload);
checkAppKeySetup();
mSubmit=(按钮)findviewbyd(R.id.auth_按钮);
mSubmit.setBackgroundColor(Color.rgb(175246179));
按钮mEmail=(按钮)findViewById(R.id.email_按钮);
背景颜色(Color.rgb(175246179));
mSubmit.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//如果您已登录,则会注销,反之亦然
如果(mLoggedIn){
注销();
}否则{
//启动远程身份验证
mApi.getSession().startAuthentication(Dropboxupload.this);
}
}
});
mSubmit.setVisibility(View.INVISIBLE);
mDisplay=(LinearLayout)findViewById(R.id.logged\u in\u display);
//这是显示照片的地方
mImage=(ImageView)findviewbyd(R.id.image\u视图);
//显示正确的UI状态(如果已登录或未登录)
setLoggedIn(mApi.getSession().isLinked());
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
putString(“mCameraFileName”,mCameraFileName);
super.onSaveInstanceState(超出状态);
}
@凌驾
受保护的void onResume(){
super.onResume();
AndroidAuthSession session=mApi.getSession();
//下一部分必须插入到的onResume()方法中
//从中调用session.startAuthentication()的活动,因此
//Dropbox身份验证正确完成。
if(session.authenticationSuccessful()){
试一试{
//完成身份验证的强制调用
session.finishAuthentication();
//将其本地存储在我们的应用程序中,供以后使用
TokenPair tokens=session.getAccessTokenPair();
存储密钥(tokens.key,tokens.secret);
setLoggedIn(真);
}捕获(非法状态){
showToast(“无法通过Dropbox进行身份验证:+e.getLocalizedMessage());
Log.i(标签“错误验证”,e);
}
}
}
//这就是在完成要导入的媒体片段时所需要的
public void uploadData(){
字符串文件名=Scene.name;
filename.replaceAll(“.txt”,”);
filename.replaceAll(“.pdf”
public void uploadFileToDropbox(File file, String uploadPath){
mApi.putFile(uploadPath, new FileInputStream(file), file.length(), null, null);
}
final static private String APP_KEY = "xxxxxxxxx";
final static private String APP_SECRET = "xxxxxxxxx";