2012-03-22 7 views
19

के साथ फ़ाइल अपलोड करने के लिए ड्रॉपबॉक्स एपीआई का उपयोग करना ड्रॉपबॉक्स एपीआई का उपयोग करके ड्रॉपबॉक्स में एक फ़ाइल (ग्राफ़िक, ऑडियो और वीडियो फ़ाइल) को कैसे अपलोड कर सकता है? मैंने Dropbox SDK Android पृष्ठ पर ट्यूटोरियल का पालन किया और नमूना काम करने के लिए प्राप्त कर सकता था। लेकिन अब एक स्ट्रिंग के बजाय मैं एक वास्तविक फ़ाइल ऑब्जेक्ट अपलोड करना चाहता हूं और संघर्ष कर रहा हूं।एंड्रॉइड

नमूना कोड बिना किसी समस्या के काम करता है और इस तरह दिखता है:

String fileContents = "Hello World!"; 
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes()); 
try { 
    Entry newEntry = mDBApi.putFile("/testing_123456.txt", inputStream, fileContents.length(), null, null); 
} catch (DropboxUnlinkedException e) { 
    Log.e("DbExampleLog", "User has unlinked."); 
} catch (DropboxException e) { 
    Log.e("DbExampleLog", "Something went wrong while uploading."); 
} 

लेकिन जब मैंने उसे बदल सकते हैं और इस कोड के साथ एक वास्तविक फ़ाइल अपलोड करने की कोशिश:

File tmpFile = new File(fullPath, "IMG_2012-03-12_10-22-09_thumb.jpg"); 

// convert File to byte[] 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
ObjectOutputStream oos = new ObjectOutputStream(bos); 
oos.writeObject(tmpFile); 
bos.close(); 
oos.close(); 
byte[] bytes = bos.toByteArray(); 

ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 
try { 
    Entry newEntry = mDBApi.putFile("/IMG_2012-03-12_10-22-09_thumb.jpg", inputStream, tmpFile.length(), null, null); 
} catch (DropboxUnlinkedException e) { 
    Log.e("DbExampleLog", "User has unlinked."); 
} catch (DropboxException e) { 
    Log.e("DbExampleLog", "Something went wrong while uploading."); 
} 

मुझे कोई सफलता DropboxException त्रुटि प्राप्त करना। मुझे लगता है कि फ़ाइल ऑब्जेक्ट को बाइट-स्ट्रीम में कनवर्ट करने का प्रयास करने वाला कुछ गलत होना चाहिए लेकिन यह सिर्फ एक धारणा है।

स्ट्रिंग उदाहरण के अलावा एंड्रॉइड के लिए ड्रॉपबॉक्स पेज पर कुछ और दस्तावेज नहीं है।

किसी भी मदद के लिए धन्यवाद।

उत्तर

23

मैं समाधान नहीं मिला - अगर किसी को भी यहां रुचि रखता है काम कर कोड है:

private DropboxAPI<AndroidAuthSession> mDBApi;//global variable 

File tmpFile = new File(fullPath, "IMG_2012-03-12_10-22-09_thumb.jpg"); 

FileInputStream fis = new FileInputStream(tmpFile); 

      try { 
       DropboxAPI.Entry newEntry = mDBApi.putFileOverwrite("IMG_2012-03-12_10-22-09_thumb.jpg", fis, tmpFile.length(), null); 
      } catch (DropboxUnlinkedException e) { 
       Log.e("DbExampleLog", "User has unlinked."); 
      } catch (DropboxException e) { 
       Log.e("DbExampleLog", "Something went wrong while uploading."); 
      } 
+7

इस कोड में mDBApi क्या है का उपयोग करता है? – TharakaNirmana

+1

मुझे पता है कि यह उत्तर देर हो चुकी है लेकिन कौन जानता है कि यह कुछ लोगों को बचा सकता है। यह एक वैश्विक चर है।यह वह कोड है जिसे आपको जोड़ना चाहिए: निजी DropboxAPI mDBApi; – Yenthe

+0

निजी DropboxAPI mDBApi; – nikki

2

@ ई-प्रकृति के जवाब सही से अधिक है ... बस मैं ड्रॉपबॉक्स की आधिकारिक साइट के लिए हर किसी को बात होगी सोचा था कि दिखाता है कि upload a file and much more कैसे करें।

इसके अलावा, @ ई-प्रकृति का उत्तर उसी नाम से फ़ाइलों को ओवरराइट करता है, इसलिए यदि आप नहीं चाहते हैं कि यह व्यवहार .putFileOverwrite के बजाय .putFile का उपयोग करें। .putFile में एक अतिरिक्त तर्क है, आप बस अंत में शून्य जोड़ सकते हैं। More info

5

यहाँ अपलोड और डाउनलोड एक फाइल करने के लिए ड्रॉपबॉक्स एपीआई का एक और कार्यान्वयन है। इसे किसी भी प्रकार की फ़ाइल के लिए लागू किया जा सकता है।

String file_name = "/my_file.txt"; 
String file_path = Environment.getExternalStorageDirectory() 
     .getAbsolutePath() + file_name; 
AndroidAuthSession session; 

public void initDropBox() { 

    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
    session = new AndroidAuthSession(appKeys); 
    mDBApi = new DropboxAPI<AndroidAuthSession>(session); 
    mDBApi.getSession().startOAuth2Authentication(MyActivity.this); 

} 

Entry response; 

public void uploadFile() { 
    writeFileContent(file_path); 
    File file = new File(file_path); 
    FileInputStream inputStream = null; 
    try { 
     inputStream = new FileInputStream(file); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    try { 
     response = mDBApi.putFile("/my_file.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(); 

    } 

} 
public void downloadFile() { 

    File file = new File(file_path); 
    FileOutputStream outputStream = null; 

    try { 
     outputStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    DropboxFileInfo info = null; 
    try { 
     info = mDBApi.getFile("/my_file.txt", null, outputStream, null); 



     Log.i("DbExampleLog", "The file's rev is: " 
       + info.getMetadata().rev); 
    } catch (DropboxException e) { 
     // TODO Auto-generated catch block 

     e.printStackTrace(); 
    } 

} 

@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     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(); 

      /** 
      * You'll need this token again after your app closes, so it's 
      * important to save it for future access (though it's not shown 
      * here). If you don't, the user will have to re-authenticate 
      * every time they use your app. A common way to implement 
      * storing keys is through Android's SharedPreferences API. 
      */ 

     } catch (IllegalStateException e) { 
      Log.i("DbAuthLog", "Error authenticating", e); 
     } 
    } 
} 

-> कॉल uploadFile() और DownloadFile() बच्चे सूत्र में विधि अन्यथा यह आप अपवाद दे देंगे

-> कि उपयोग AsyncTask के लिए और doInBackground में विधि ऊपर इन फोन तरीका।

आशा यह उपयोगी हो जाएगा ... धन्यवाद

2

यहाँ एक और उदाहरण है जो ड्रॉपबॉक्स v2 API लेकिन एक 3 पार्टी एसडीके का उपयोग करता है। यह वैसे भी Google ड्राइव, OneDrive और Box.com के लिए बिल्कुल वही काम करता है।

// CloudStorage cs = new Box(context, "[clientIdentifier]", "[clientSecret]"); 
// CloudStorage cs = new OneDrive(context, "[clientIdentifier]", "[clientSecret]"); 
// CloudStorage cs = new GoogleDrive(context, "[clientIdentifier]", "[clientSecret]"); 
CloudStorage cs = new Dropbox(context, "[clientIdentifier]", "[clientSecret]"); 
new Thread() { 
    @Override 
    public void run() { 
     cs.createFolder("/TestFolder"); // <--- 
     InputStream stream = null; 
     try { 
      AssetManager assetManager = getAssets(); 
      stream = assetManager.open("UserData.csv"); 
      long size = assetManager.openFd("UserData.csv").getLength(); 
      cs.upload("/TestFolder/Data.csv", stream, size, false); // <--- 
     } catch (Exception e) { 
      // TODO: handle error 
     } finally { 
      // TODO: close stream 
     } 
    } 
}.start(); 

यह CloudRail Android SDK