安卓 java 映射目录_【已解决】Android中实现对应的文件夹选择

【问题】

想要给一个Android的app:downloadSongtasteMusic,添加一个文件夹选择的功能,供用户选择下载下来的文件存放到何处。

【解决过程】

1.去Widget中,没有找到类似的控件:

widgets-no-found-file-chooser_thumb.png

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

}

}

}

}

然后再去尝试新建一个布局文件:

91fd53778b7a1ab7e3258e42245d9009.png

ec6b1a7e6a00e1c57b64d6df24c8b82d.png

然后就可以新建出一个新的布局文件了:

8031dec5eeff6223553bb2e9b388d889.png

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"

/>

效果如下:

0f11eb655657f9aaaf2eb9a04a2546e6.png

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.期间,遇到一个问题,解决过程参见:

至此,终于实现了,基本的,可以实现界面切换了,并且可以显示出对应的文件列表了:

0ebb6548a8f9e6d564c6316169f23240.png

目前的返回,只能通过点击左上角的图标才能返回。

余下的,就是如何实现对应的文件夹选择,然后点击OK去返回了。

14.再继续参考:

去添加双击某个文件夹时的行为,和单击选择某个文件夹时的行为。

15.继续去学习:

经过一番折腾,最后终于实现了基本的,可以工作的代码了。

【总结】

目前,基本实现了,虽然很丑,但是能用的,文件夹浏览的功能。

运行时效果如下:

7b635c5a8d0191e3baa0f513612574c7.png

具体实现中:

几个相关的文件为:

8ae8351de4beb3d3c90fcd86827bb450.png

核心的代码如下:

/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

总的来说,折腾这东西,还是蛮耗精力的。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值