Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
Demo B.
10 years agoNew member | Level 1
How to access a directory programatically with java android code in dropbox
Not able to provide the correct path to upload a file programatically in dropbox.
putfile() can be used..But the path i am not able to provide . Searching for last 4 hours.Not able to find a soluti...
Demo B.
10 years agoNew member | Level 1
package com.example.mhk.dropbox;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.session.AppKeyPair;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
{
private DropboxAPI<AndroidAuthSession> mDBApi;//global variable
final static private String APP_KEY = "jkj";
final static private String APP_SECRET = "xyx";
class forPutFile extends AsyncTask<String,String,Boolean>
{
@Override
protected Boolean doInBackground(String... params)
{
Log.d("mhk","indoInBackground");
File file = new File(Environment.getExternalStorageDirectory()+"/hello.txt");
FileInputStream inputStream=null;
try
{
inputStream = new FileInputStream(file);
Log.d("mhk","file created..");
Log.d("mhk","inputstream is :"+inputStream);
}
catch (FileNotFoundException e)
{
Log.e("mhk", "inputstreamException is : " + e.getMessage());
}
try
{
DropboxAPI.Entry response = mDBApi.putFile("/hello.txt", inputStream,file.length(), null, null);
Log.d("mhk","The uploaded file's rev is: " + response.rev);
}
catch (DropboxException e)
{
e.printStackTrace();
Log.e("mhk", "dropboxException is :" + e.getMessage());
}
return false;
}
}
void savingToDropbox()
{
try
{
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
}
catch (Exception e1)
{
Log.e("mhk", "Exception" + e1.getMessage());
}
}
protected void onResume()
{
super.onResume();
if (mDBApi.getSession().authenticationSuccessful())
{
try
{
// Required to complete auth, sets the access token on the session
mDBApi.getSession().finishAuthentication();
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
SharedPreferences sp=getSharedPreferences("DropBox App", MODE_PRIVATE);
SharedPreferences.Editor ed=sp.edit();
ed.putString("accessToken",accessToken);
}
catch (IllegalStateException e)
{
Log.e("mhk", "Error authenticating", e);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savingToDropbox();
new forPutFile().execute("Hii");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is the full code snippet i am using. Gregory had done by giving simple path also.Getting the same error giving under:
11-16 14:49:57.977 22637-22675/? D/mhk﹕ indoInBackground
11-16 14:49:57.987 22637-22675/? D/mhk﹕ file created..
11-16 14:49:57.987 22637-22675/? D/mhk﹕ inputstream is :java.io.FileInputStream@41aba9f0
11-16 14:49:57.997 22637-22675/? E/mhk﹕ dropboxException is :null (This is the exception...)
Please provide a solution .I am studying Core API for the first time.And it had really irritated me.
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!