0

我有一个应用程序和相关数据,我可以将这些数据从我的Assest文件夹复制到SD 卡上,这是完美的。但我想改进此代码并允许用户指定 SD 卡上的位置,如她想要存储数据的位置。这是我的代码,请提出修改建议。(我想生成一个类似的弹出窗口,当从互联网下载文件并且弹出窗口要求用户指定下载位置时,我很困惑是我是否必须自定义制作这个弹出窗口或者它是内置)以下是我的代码:

  private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          out = new FileOutputStream("/sdcard/edu/" + filename);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }       
    }
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
public void MakeFolder(){
File direct = new File(Environment.getExternalStorageDirectory() + "/edu");

if(!direct.exists())
 {
     if(direct.mkdir()){ //directory is created;
         copyAssets();
 }
}
4

2 回答 2

2

在这种情况下,基于 AlertDialog 选择目录或文件的 Android 对话框将为您提供帮助。

http://www.ulduzsoft.com/2012/07/android-dialog-to-choose-a-directory-or-file-based-on-alertdialog/

于 2012-12-31T09:34:37.097 回答
0

这是我准备的最终代码(来自谷歌的有价值的输入):

public class Fexplorer extends ListActivity {


String B=null ;
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.explorer_main);
    myPath = (TextView)findViewById(R.id.path);
    Button OK = (Button)findViewById(R.id.button2);
    Button Cancel = (Button)findViewById(R.id.button1);


    OK.setOnClickListener(onOK);
    Cancel.setOnClickListener(onCancel);

    root = Environment.getExternalStorageDirectory().getPath();

    getDir(root);
}

private void getDir(String dirPath)
{
    myPath.setText("Choose Location");
    B = dirPath; 
    Toast.makeText(getApplicationContext(),B, Toast.LENGTH_LONG).show();
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles();

    if(!dirPath.equals(root))
    {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());    
    }

    for(int i=0; i < files.length; i++)
    {
        File file = files[i];

        if(!file.isHidden() && file.canRead()){
            path.add(file.getPath());
            if(file.isDirectory()){
                item.add(file.getName() + "/");
            }else{
                item.add(file.getName());
            }
        }   
    }

    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead()){
            getDir(path.get(position));
        }else{
            new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "] folder can't be read!")
                .setPositiveButton("OK", null).show();  
        }   
    }

    /*else {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "]")
                .setPositiveButton("OK", null).show();

      } */
}

 private View.OnClickListener onOK=new View.OnClickListener() {
        public void onClick(View v){


        copy();
        Intent About = new Intent(Fexplorer.this,EduBridge.class);
        startActivity(About);

        }
    };

 private View.OnClickListener onCancel=new View.OnClickListener() {
        public void onClick(View v){


            Intent About = new Intent(Fexplorer.this,EduBridge.class);
            startActivity(About);


        }
    };

    public void copy(){
                try {
                        File sourceFile = new File("/sdcard/edu/Brochure.pdf");
                        File destinationFile = new File(B+"/" + sourceFile.getName());
                        String X = (B+"/" + sourceFile.getName());
                        Toast.makeText(getApplicationContext(),X, Toast.LENGTH_LONG).show();
                        Fexplorer copyFileExample = new Fexplorer();
                        copyFileExample .copyFile(sourceFile, destinationFile);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public void copyFile(File sourceFile, File destinationFile) {
                try {
                        FileInputStream fileInputStream = new FileInputStream(sourceFile);
                        FileOutputStream fileOutputStream = new FileOutputStream(
                                        destinationFile);

                        int bufferSize;
                        byte[] bufffer = new byte[512];
                        while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
                                fileOutputStream.write(bufffer, 0, bufferSize);
                        }

                        fileInputStream.close();
                        fileOutputStream.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

    }

在样式中:

    <style name="MyFloatingWindow" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>

explorer_main XML 文件:

<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" 
android:orientation="vertical"
android:layout_gravity ="center">

<TextView
    android:id="@+id/path"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
     />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_below="@+id/path"
    android:layout_height="308dp" />

<TextView
    android:id="@android:id/empty"
    android:layout_below="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="No Data"
    />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@android:id/empty"

    android:text="@string/DirB" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="true"

    android:text="@string/DirA" />

最后是 row.xml 文件:

     <?xml version="1.0" encoding="utf-8"?>
     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/rowtext"
     android:layout_width="fill_parent"
     android:layout_height="50sp"
     android:textSize="15dp" />
于 2013-01-03T03:57:00.763 回答