【问题】
想要给一个Android的app:downloadSongtasteMusic,添加一个文件夹选择的功能,供用户选择下载下来的文件存放到何处。
【解决过程】
1.去Widget中,没有找到类似的控件:
2.找了半天,都是关于文件选择的,而不是文件夹的选择,虽然两者类似,但是还是懒得弄。
3.后来找到一个,关于文件夹选择的:
然后去试试。
结果也是一对错误,即使导入了很多库之后,还是无法正常编译。
看起来,貌似不是一下子就能完全理解的。
4.难不成,真的要像:
中所说的,要自己去实现对应的代码了???
算了,还是自己按照自己的逻辑,一点点去实现代码吧。
5.不过又找到一些其他的例子:
所以还是先参考:
去添加自己的代码。
6.先写了点代码: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.然后去添加对应的布局设置代码:<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="400dp"
android:orientation="vertical"
>
android:id="@+id/mPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="18sp"
>
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="330dp"
>
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:id="@+id/buttonConfirm"
android:layout_width="125dp"
android:layout_height="fill_parent"
android:text="OK"
/>
android:id="@+id/buttonCancle"
android:layout_width="125dp"
android:layout_height="fill_parent"
android:text="Cancel"
/>
效果如下:
8.不过,又看到了另外一处参考资料,更简洁:
所以打算先试试那个简单的。
结果竟然还是一堆错误,哎。。。
9.折腾了半天,还是不行。
其中涉及几个问题,自己解决了:
10.现在遇到的问题是,主函数内写完代码后,可以执行到:/** 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.从
找到:
继续去学习。。。
12.后来经过一些学习后,再加上去折腾:
然后才大概搞懂了Activity的一些逻辑。
具体总结,详见:
13.期间,遇到一个问题,解决过程参见:
至此,终于实现了,基本的,可以实现界面切换了,并且可以显示出对应的文件列表了:
目前的返回,只能通过点击左上角的图标才能返回。
余下的,就是如何实现对应的文件夹选择,然后点击OK去返回了。
14.再继续参考:
去添加双击某个文件夹时的行为,和单击选择某个文件夹时的行为。
15.继续去学习:
经过一番折腾,最后终于实现了基本的,可以工作的代码了。
【总结】
目前,基本实现了,虽然很丑,但是能用的,文件夹浏览的功能。
运行时效果如下:
具体实现中:
几个相关的文件为:
核心的代码如下:
/DownloadSongtasteMusic/src/crifan/com/downloadsongtastemusic/MainActivity.javapackage 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.javapackage 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 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 folderItemList = new ArrayAdapter(this, R.layout.file_list_row, folderNameList);
setListAdapter(folderItemList);
}
private List getFolerStrList(File curPath){
List folerNameList = new ArrayList();
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:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
android:id="@+id/btnRoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="browseRoot"
android:text="Root" />
android:id="@+id/btnUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="browseUp"
android:text="Up" />
android:id="@+id/txvCurPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#C3BFF9"
android:textSize="16sp" >
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="248dp"
android:layout_weight="0.41" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="ChooseFolerOk"
android:text="OK" />
android:id="@+id/btnCancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="ChooseFolerCancel"
android:text="Cancel" />
/DownloadSongtasteMusic/res/layout/file_list_row.xml<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
/DownloadSongtasteMusic/res/values/strings.xml<?xml version="1.0" encoding="utf-8"?>
...
Directory Browser
总的来说,折腾这东西,还是蛮耗精力的。。。