Shah Paneri
Shah Paneri

Reputation: 739

Dropbox and Android - get Files names via API call

Does anyone know how to get metadata list...I mean Files and Folder which exist in our app folder in dropbox in Android.i need to get File and Folder names.. Now again i want to create Folder in Dropbox from android.i reffered dropbox docs.but didn't find same in that.. Thanx in advance.

Upvotes: 4

Views: 5757

Answers (4)

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

If you want to retrieve all FILES and FOLDER from path, then use this code, it will help you.

I got solution by myself.

    String mPath="/";  //You can change the path here to specific FOLDER
Entry dirent = null;
 try 
   {
      dirent = mApi.metadata(mPath, 1000, null, true, null);            
    } 
 catch (DropboxException e)
   {
     System.out.println("Error Detail "+e.getMessage());
   }

//Perform a Loop and retrieve all FILES and FOLDER from the PATH

     for (Entry ent: dirent.contents)
     {
       String name = ent.fileName();                           
       System.out.println("My File in Folder "+name);                        
      }  

Upvotes: 0

Akanksha Rathore
Akanksha Rathore

Reputation: 3623

Entry entries = mApi.metadata(Path, 100, null, true, null);

for (Entry e : entries.contents) {
    if (!e.isDeleted) {
        Log.i("Is Folder",String.valueOf(e.isDir));
        Log.i("Item Name",e.fileName());
    }
}

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

I had the same problems, and find it out. After access is done, create a new Entry as described above, but set the parameter "boolean list" to true. If you had done this,List will not be null, and You can get the information you want with a for-loop

AccessTokenPair access = getStoredKeys();
mDBApi.getSession().setAccessTokenPair(access);

try {
    // ----SET boolean list PARAMETER TO TRUE---------

    Entry dropboxDir = mDBApi.metadata("/YOUR_FOLDER_NAME/", 0, null, true, null);
    if (dropboxDir.isDir) {
        List<Entry> contents = dropboxDir.contents;
        if (contents != null) {
            // ------CREATE NEW ENTRY AND THEN, GET THE FILENAME OF
            // EVERY FILE
            for (int i = 0; i < contents.size(); i++) {
                Entry e = contents.get(i);
                String a = e.fileName();
                Log.d("dropbox", "FileName:" + a);
            }
        }
    }
    // ONLY A TEST CATCH, PLEASE DO WRIGHT EXCEPTION HANDLING HERE
} catch (Exception ex) {
    Log.d("dropbox", "ERROR");
}

Upvotes: 2

βhargavḯ
βhargavḯ

Reputation: 9836

Here what i have done and it works fine for me.

SavedProperties.selectedAddress is my static variable which contains name of folder to be created.

Here i am

1) checking whether folder exists.

2) create folder if does not exist.

3) upload files to that folder.

    private void loadMetadata() 
    {


        // Metadata
        try 
        {
            Entry existingEntry = mApi.metadata("/" + SavedProperties.selectedAddress , 1, null, false, null);
            if(existingEntry.isDir)
            {
                Log.d(TAG, "Folder exists : " + existingEntry.fileName());
                uploadPictures("/"+SavedProperties.selectedAddress + "/");
            }
        } 
        catch (DropboxException  e) 
        {
            Log.d(TAG,"Folder does not exist..." + e.fillInStackTrace());
            try 
            {
                Entry createFolder = mApi.createFolder("/"+SavedProperties.selectedAddress);
                Log.d(TAG,"Folder created..." + createFolder.rev);
                uploadPictures("/"+SavedProperties.selectedAddress + "/");
            } 
            catch (DropboxException e1)
            {
                 Log.d(TAG,"Create Folder DropboxException : " + e1.fillInStackTrace() );
            }       
        }
    }

    private void uploadPictures(String uploadTo) 
    {
        // Uploading Pictures....
        FileInputStream inputStream = null;
        try 
        {
            for(int i =0 ; i < selectedImages.length; i++)
            {
                if(selectedImages[i]==0)
                {
                    String path = PictureGallery.images.get(i).toString().replace("file://", "");
                    String filename = PictureGallery.images.get(i).toString().split("/")[PictureGallery.images.get(i).toString().split("/").length-1];
                    File file = new File(path);
                    inputStream = new FileInputStream(file);
                    Entry newEntry = mApi.putFile(uploadTo + filename, inputStream, file.length(), null, null);
                    Log.i(TAG, "The uploaded file's rev is: " + newEntry.rev);
                    //Log.d(TAG,"PictureGallery " + PictureGallery.images.get(i).toString().split("/")[PictureGallery.images.get(i).toString().split("/").length-1]);
                }
            }
            progress.dismiss();
        } catch (DropboxUnlinkedException e) {
            // User has unlinked, ask them to link again here.
            Log.e(TAG, "User has unlinked.");
        } catch (DropboxException e) {
            Log.e(TAG, "Something went wrong while uploading.");
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found.");
        } 
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {}
            }
        }
    }

i have used android sdk 1.3.1 in my case.

Upvotes: 3

Related Questions