usar subir sirve que para instrucciones imagenes herramientas guardar gratis google descargar como celular cargas camara archivos archivo app aplicacion android inputstream dropbox dropbox-api android-file

subir - Usando la API de Dropbox para cargar un archivo con Android



subir a dropbox (4)

Aquí hay otra implementación de la API de Dropbox para cargar y descargar un archivo. Esto se puede implementar para cualquier tipo de archivo.

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); } } }

-> Llame al método uploadFile () y downLoadFile () en un subproceso secundario, de lo contrario le dará una excepción

-> Para eso use AsyncTask y llame a estos métodos anteriores en el método doInBackground.

Espero que esto sea de ayuda ... Gracias

¿Cómo puedo cargar un archivo (archivo de gráficos, audio y video) con Android usando la API de Dropbox a Dropbox? Seguí el tutorial en la página de Android de Dropbox SDK y pude hacer que la muestra funcionara. Pero ahora, en lugar de una cadena, quiero cargar un objeto de archivo real y estoy luchando.

El código de muestra funciona sin problemas y se ve así:

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."); }

Pero cuando intento cambiarlo y subir un archivo real con este código:

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."); }

No tengo éxito obteniendo un error DropboxException. Creo que algo en lo que trato de convertir el objeto de Archivo al flujo de bytes debe estar mal, pero esto es solo una suposición.

Aparte del ejemplo de String, no hay nada más documentado en la página de Dropbox para Android.

Gracias por cualquier ayuda.


Aquí hay otro ejemplo que utiliza la API de Dropbox v2 pero un SDK de terceros. Por cierto, funciona exactamente igual para Google Drive, OneDrive y 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();

Utiliza el SDK de Android CloudRail


Encontré la solución: si alguien está interesado, aquí está el código de trabajo:

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."); }


La respuesta de @ e-nature es más que correcta ... solo pensé en señalar a todos al sitio oficial de Dropbox que muestra cómo cargar un archivo y mucho más .

Además, la respuesta de @ e-nature sobrescribe los archivos con el mismo nombre, por lo que si no desea ese comportamiento, simplemente utilice .putFile lugar de .putFileOverwrite . .putFile tiene un argumento adicional, simplemente puede agregar null al final. Mas informacion