2014-02-19 23 views
0

我正在構建一個應用程序來顯示Dropbox帳戶中的文件,如果她選擇的話,這也將允許用戶在本地下載文件。 Dropbox的內容將顯示在一個標籤片段中!如何在android應用中的Dropbox中顯示文件?

它成功鏈接到保管箱帳戶,但不顯示文件,也凍結了,因爲我在UI類中執行網絡操作。

我有兩個問題.. 1.如何使用的AsyncTask在我的代碼 2.如何顯示文件,並給用戶一個選擇下載在本地都在一個標籤片段?

我是新來的android開發,任何其他技巧將不勝感激。 在此先感謝。

MainActivity代碼..

public class MainActivity extends ActionBarActivity { 

    final static private String APP_KEY = "key_here"; 
    final static private String APP_SECRET = "secret_here"; 
    final static private AccessType ACCESS_TYPE = AccessType.DROPBOX; 
    private static final boolean USE_OAUTH1 = false; 

    // 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"; 


    // In the class declaration section; 
    protected DropboxAPI<AndroidAuthSession> mDBApi; 
    static String[] fnames; 


    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     // Notice that setContentView() is not used, because we use the root 
     // android.R.id.content as the container for each fragment 


     // setup action bar for tabs 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     actionBar.setDisplayShowTitleEnabled(true); 

     final String info = "Info"; 
     final String db = "Dropbox"; 
     Tab tab = actionBar.newTab() 
          .setText(info) 
          .setTabListener(new TabListener<InfoFragment>(
            this,"info", InfoFragment.class)); 
     actionBar.addTab(tab); 

     tab = actionBar.newTab() 
         .setText(db) 
         .setTabListener(new TabListener<DbFragment>(
           this, "dropbox", DbFragment.class)); 
     actionBar.addTab(tab); 

    // And later in some initialization function: 
     AndroidAuthSession session = buildSession(); 
     mDBApi = new DropboxAPI<AndroidAuthSession>(session); 


     if (USE_OAUTH1){ 
      mDBApi.getSession().startAuthentication(MainActivity.this); 
     } else{ 
      mDBApi.getSession().startOAuth2Authentication(MainActivity.this); 
     } 

     int i = 0; 
     fnames = null; 
     Entry entries; 
     ArrayList<Entry> files = new ArrayList<Entry>(); 
     ArrayList<String> dir = new ArrayList<String>(); 
     try { 
      entries = mDBApi.metadata("/", 100, null, true, null); 
      for (Entry e: entries.contents){ 
       if (!e.isDeleted){ 
        files.add(e); 
        dir.add(new String(files.get(i++).path)); 
       } 
      } 
     } catch (DropboxException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     fnames = dir.toArray(new String[dir.size()]); 
    } 





    public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 
     private Fragment mFragment; 
     private final Activity mActivity; 
     private final String mTag; 
     private final Class<T> mClass; 

     /** Constructor used each time a new tab is created. 
      * @param activity The host Activity, used to instantiate the fragment 
      * @param tag The identifier tag for the fragment 
      * @param clz The fragment's Class, used to instantiate the fragment 
      */ 
     public TabListener(Activity activity, String tag, Class<T> clz) { 
      mActivity = activity; 
      mTag = tag; 
      mClass = clz; 
     } 

     /* The following are each of the ActionBar.TabListener callbacks */ 

     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      // Check if the fragment is already initialized 
      if (mFragment == null) { 
       // If not, instantiate and add it to the activity 
       mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
       ft.add(android.R.id.content, mFragment, mTag); 
      } else { 
       // If it exists, simply attach it in order to show it 
       ft.attach(mFragment); 
      } 
     } 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      if (mFragment != null) { 
       // Detach the fragment, because another one is being attached 
       ft.detach(mFragment); 
      } 
     } 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // User selected the already selected tab. Usually do nothing. 
     } 
    } 

    @Override 
    public 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(); 
      } catch (IllegalStateException e){ 
       Log.i("DBAuthLog", "Error authenticating",e); 
      } 
     } 
    } 

    private void loadAuth(AndroidAuthSession session) { 
     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 || key.length() == 0 || secret.length() == 0) return; 

     if (key.equals("oauth2:")) { 
      // If the key is set to "oauth2:", then we can assume the token is for OAuth 2. 
      session.setOAuth2AccessToken(secret); 
     } else { 
      // Still support using old OAuth 1 tokens. 
      session.setAccessTokenPair(new AccessTokenPair(key, secret)); 
     } 
    } 


    /** 
    * 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 storeAuth(AndroidAuthSession session) { 
     // Store the OAuth 2 access token, if there is one. 
     String oauth2AccessToken = session.getOAuth2AccessToken(); 
     if (oauth2AccessToken != null) { 
      SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
      Editor edit = prefs.edit(); 
      edit.putString(ACCESS_KEY_NAME, "oauth2:"); 
      edit.putString(ACCESS_SECRET_NAME, oauth2AccessToken); 
      edit.commit(); 
      return; 
     } 
     // Store the OAuth 1 access token, if there is one. This is only necessary if 
     // you're still using OAuth 1. 
     AccessTokenPair oauth1AccessToken = session.getAccessTokenPair(); 
     if (oauth1AccessToken != null) { 
      SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
      Editor edit = prefs.edit(); 
      edit.putString(ACCESS_KEY_NAME, oauth1AccessToken.key); 
      edit.putString(ACCESS_SECRET_NAME, oauth1AccessToken.secret); 
      edit.commit(); 
      return; 
     } 
    } 

    private AndroidAuthSession buildSession() { 
     AppKeyPair appKeyPair = new AppKeyPair(MainActivity.APP_KEY, MainActivity.APP_SECRET); 

     AndroidAuthSession session = new AndroidAuthSession(appKeyPair); 
     loadAuth(session); 
     return session; 
    } 

} 

收存箱片段代碼

public class DbFragment extends ListFragment { 

    private ListView mListView; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 


     String[] xfnames = MainActivity.fnames; 


     ListView listView = new ListView(getActivity()); 
     ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, xfnames); 

     for (String str: xfnames) 
      array.add(str); 
     setListAdapter(array); 



     return super.onCreateView(inflater, container, savedInstanceState); 
    } 




} 

主要活動XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello"> 
     android:layout_marginTop="40dp" 
     android:textSize="40sp"/> 
    </TextView> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    </ListView> 


</LinearLayout> 

收存箱片段XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    </ListView> 


</RelativeLayout> 
+0

一個相當廣泛的問題在這裏,但解決您的問題之一,看看這個靈感就從下載Dropbox的文件(這是使用同步API)http://stackoverflow.com/questions/ 20948156/Dropbox的同步-API-Android的更新緩存,文件/ 20951592#20951592 – cYrixmorten

回答

0

如果您想要在列表視圖中顯示下拉框的文件和文件夾,只需在您的項目中使用此代碼即可解決您的問題。

private void PopulateList() 
     { 
      List<String> filename = new ArrayList<String>(); 
      String mPath = "/"; 
      Entry dirent = null; 
      try 
      { 
       dirent = mApi.metadata(mPath, 1000, null, true, null); 
      } catch (DropboxException e) 
      { 
       System.out.println("Error : "+e.getMessage()); 
      } 
      for (Entry ent: dirent.contents) 
      { 
       filename.add(ent.fileName()); 

     //Putting all these FILES & FOLDER in LIST VIEW 
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1, filename); 
      mListView.setAdapter(arrayAdapter); 

     } 
相關問題