【问题】
想要给一个Android的app:downloadSongtasteMusic,添加一个文件夹选择的功能,供用户选择下载下来的文件存放到何处。
【解决过程】
1.去Widget中,没有找到类似的控件:
2.找了半天,都是关于文件选择的,而不是文件夹的选择,虽然两者类似,但是还是懒得弄。
3.后来找到一个,关于文件夹选择的:
Android dialog to choose a directory or file based on AlertDialog
然后去试试。
结果也是一对错误,即使导入了很多库之后,还是无法正常编译。
看起来,貌似不是一下子就能完全理解的。
4.难不成,真的要像:
Allow a user to select a folder on an SD card
中所说的,要自己去实现对应的代码了???
算了,还是自己按照自己的逻辑,一点点去实现代码吧。
5.不过又找到一些其他的例子:
Android入门:弹出框选择文件夹目录 以及启用新的Task打开文件
所以还是先参考:
Android入门:弹出框选择文件夹目录 以及启用新的Task打开文件
去添加自己的代码。
6.先写了点代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import android.content.Intent; public class MainActivity extends Activity { public static final int FOLDER_RESULT_CODE = 1 ; /*********************************************************** * for Folder Chooser ***********************************************************/ /** Choose folder for downloaded music file to save */ public void ChooseFoler(View view) { Intent intent = new Intent(MainActivity. this , FolderChooser. class ); startActivityForResult(intent, FOLDER_RESULT_CODE); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (FOLDER_RESULT_CODE == requestCode){ Bundle bundle = null ; if (data!= null &&(bundle=data.getExtras())!= null ){ EditText etSaveTo = (EditText) findViewById(R.id.saveTo); etSaveTo.setText(bundle.getString( "file" )); } } } } |
然后再去尝试新建一个布局文件:
然后就可以新建出一个新的布局文件了:
7.然后去添加对应的布局设置代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "250dp" android:layout_height = "400dp" android:orientation = "vertical" > < TextView android:id = "@+id/mPath" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "5dp" android:textSize = "18sp" > </ TextView > < ListView android:id = "@android:id/list" android:layout_width = "fill_parent" android:layout_height = "330dp" > </ ListView > < LinearLayout android:gravity = "center" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "horizontal" > < Button android:id = "@+id/buttonConfirm" android:layout_width = "125dp" android:layout_height = "fill_parent" android:text = "OK" /> < Button android:id = "@+id/buttonCancle" android:layout_width = "125dp" android:layout_height = "fill_parent" android:text = "Cancel" /> </ LinearLayout > </ LinearLayout > |
效果如下:
8.不过,又看到了另外一处参考资料,更简洁:
所以打算先试试那个简单的。
结果竟然还是一堆错误,哎。。。
9.折腾了半天,还是不行。
其中涉及几个问题,自己解决了:
【已解决】Android中关于代码R.layout.main出错:main cannot be revolved or is not a field
【已解决】Android代码R.layout.main出错:R cannot be resolved to a variable
10.现在遇到的问题是,主函数内写完代码后,可以执行到:
1 2 3 4 5 6 | /** Choose folder for downloaded music file to save */ public void ChooseFoler(View view) { Intent intent = new Intent(MainActivity. this , DirectoryBrowser. class ); startActivityForResult(intent, FOLDER_RESULT_CODE); } |
但是之后,无法跳转到对应的DirectoryBrowser中的onCreate函数内。
所以,还是不清楚,界面如何跳转的。
所以,还是先去学习一下这方面的基本逻辑去吧。
11.从
http://developer.android.com/intl/zh-CN/training/index.html
找到:
Managing the Activity Lifecycle
继续去学习。。。
12.后来经过一些学习后,再加上去折腾:
然后才大概搞懂了Activity的一些逻辑。
具体总结,详见:
【整理】Android中,如何新建一个界面,并且实现从当前界面切换到到刚才新建的(另外一个)界面
13.期间,遇到一个问题,解决过程参见:
【已解决】Android代码出错:The method setListAdapter(ArrayAdapter<String>) is undefined for the type xxx
至此,终于实现了,基本的,可以实现界面切换了,并且可以显示出对应的文件列表了:
目前的返回,只能通过点击左上角的图标才能返回。
余下的,就是如何实现对应的文件夹选择,然后点击OK去返回了。
14.再继续参考:
Android入门:弹出框选择文件夹目录 以及启用新的Task打开文件
去添加双击某个文件夹时的行为,和单击选择某个文件夹时的行为。
15.继续去学习:
http://developer.android.com/intl/zh-CN/reference/android/app/ListActivity.html
经过一番折腾,最后终于实现了基本的,可以工作的代码了。
【总结】
目前,基本实现了,虽然很丑,但是能用的,文件夹浏览的功能。
运行时效果如下:
具体实现中:
几个相关的文件为:
核心的代码如下:
/DownloadSongtasteMusic/src/crifan/com/downloadsongtastemusic/MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package crifan.com.downloadsongtastemusic; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.EditText; import android.content.Intent; public class MainActivity extends Activity { public static final int REQ_FOLDER_SELECT = 1 ; /*********************************************************** * for Folder Chooser ***********************************************************/ /** Choose folder for downloaded music file to save */ public void ChooseFoler(View view) { Intent intent = new Intent(MainActivity. this , DirectoryBrowser. class ); startActivityForResult(intent, REQ_FOLDER_SELECT); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent intent) { if (REQ_FOLDER_SELECT == requestCode){ Bundle bundle = null ; if (intent != null ){ bundle=intent.getExtras(); if (bundle != null ) { if (resultCode == DirectoryBrowser.RESULT_CHOOSE_DIR_OK){ EditText etSaveTo = (EditText) findViewById(R.id.saveTo); etSaveTo.setText(bundle.getString( "selFolderPath" )); } else if (resultCode == DirectoryBrowser.RESULT_CHOOSE_DIR_CANCEL){ } } } } } } |
/DownloadSongtasteMusic/src/crifan/com/downloadsongtastemusic/DirectoryBrowser.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | package crifan.com.downloadsongtastemusic; import java.io.File; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.os.Environment; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.support.v4.app.NavUtils; //import android.app.Activity; import android.app.ListActivity; import android.content.Intent; public class DirectoryBrowser extends ListActivity { //public class DirectoryBrowser extends Activity { private String rootPath = null ; private String curAbsPath = null ; List<String> folderNameList = null ; private TextView txvCurPath; public static final int RESULT_CHOOSE_DIR_OK = 0 ; public static final int RESULT_CHOOSE_DIR_CANCEL = - 1 ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_directory_browser); // Show the Up button in the action bar. //getActionBar().setDisplayHomeAsUpEnabled(true); txvCurPath = (TextView) findViewById(R.id.txvCurPath); File extStorDir = Environment.getExternalStorageDirectory(); String extStroAbsPath = extStorDir.getAbsolutePath(); rootPath = extStroAbsPath; updateCurFolder(extStroAbsPath); } private void updateCurFolder(String newAbsPath) { curAbsPath = newAbsPath; txvCurPath.setText(curAbsPath); folderNameList = getFolerStrList( new File(curAbsPath)); ArrayAdapter<String> folderItemList = new ArrayAdapter<String>( this , R.layout.file_list_row, folderNameList); setListAdapter(folderItemList); } private List<String> getFolerStrList(File curPath){ List<String> folerNameList = new ArrayList<String>(); File[] files = curPath.listFiles(); for (File eachFile : files){ if (eachFile.isDirectory()) { folerNameList.add(eachFile.getName()); } } return folerNameList; } // @Override // public boolean onCreateOptionsMenu(Menu menu) { // // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.activity_directory_browser, menu); // return true; // } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // NavUtils.navigateUpFromSameTask( this ); return true ; } return super .onOptionsItemSelected(item); } @Override protected void onListItemClick(ListView listView, View view, int position, long id) { updateCurFolder(curAbsPath + "/" + folderNameList.get(position)); } /** return prev UI */ public void ChooseFolerOk(View v) { Intent intent = new Intent(DirectoryBrowser. this , MainActivity. class ); Bundle bundle = new Bundle(); bundle.putString( "selFolderPath" , curAbsPath); intent.putExtras(bundle); setResult(RESULT_CHOOSE_DIR_OK, intent); finish(); } public void ChooseFolerCancel(View v) { Intent intent = new Intent(DirectoryBrowser. this , MainActivity. class ); setResult(RESULT_CHOOSE_DIR_CANCEL, intent); finish(); } public void browseRoot(View v) { if (curAbsPath != rootPath) { updateCurFolder(rootPath); } } public void browseUp(View v) { if (curAbsPath == rootPath) { //do nothing when is root } else { String parentFoler = new File(curAbsPath).getParent().toString(); updateCurFolder(parentFoler); } } } |
/DownloadSongtasteMusic/res/layout/activity_directory_browser.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <!-- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" --> <!-- xmlns:tools="http://schemas.android.com/tools" --> <!-- android:layout_width="match_parent" --> <!-- android:layout_height="match_parent" --> <!-- tools:context=".DirectoryBrowser" > --> <!-- <TextView --> <!-- android:layout_width="wrap_content" --> <!-- android:layout_height="wrap_content" --> <!-- android:layout_centerHorizontal="true" --> <!-- android:layout_centerVertical="true" --> <!-- android:text="@string/hello_world" /> --> <!-- </RelativeLayout> --> android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "horizontal" > < Button android:id = "@+id/btnRoot" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_weight = "1" android:onClick = "browseRoot" android:text = "Root" /> < Button android:id = "@+id/btnUp" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_weight = "1" android:onClick = "browseUp" android:text = "Up" /> </ LinearLayout > < TextView android:id = "@+id/txvCurPath" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:padding = "5dp" android:background = "#C3BFF9" android:textSize = "16sp" > </ TextView > < ListView android:id = "@android:id/list" android:layout_width = "wrap_content" android:layout_height = "248dp" android:layout_weight = "0.41" > </ ListView > <!-- <TextView android:id="@android:id/empty" --> <!-- android:layout_width="match_parent" --> <!-- android:layout_height="match_parent" --> <!-- android:background="#FF0000" --> <!-- android:text="Nothing to show"/> --> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "horizontal" > < Button android:id = "@+id/btnOk" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_weight = "1" android:onClick = "ChooseFolerOk" android:text = "OK" /> < Button android:id = "@+id/btnCancle" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_weight = "1" android:onClick = "ChooseFolerCancel" android:text = "Cancel" /> </ LinearLayout > </ LinearLayout > |
/DownloadSongtasteMusic/res/layout/file_list_row.xml
1 2 3 4 5 6 | <? xml version = "1.0" encoding = "utf-8" ?> < TextView android:id = "@+id/text1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > </ TextView > |
/DownloadSongtasteMusic/res/values/strings.xml
1 2 3 4 5 6 7 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > ... < string name = "title_activity_directory_browser" >Directory Browser</ string > </ resources > |
总的来说,折腾这东西,还是蛮耗精力的。。。
转载请注明:在路上 » 【已解决】Android中实现对应的文件夹选择