2012-12-31 53 views
0

我有一個應用程序和它的相關數據,我可以從我的Assest文件夾複製這個數據到SD卡,這是完美無瑕的。但我想改進這些代碼,並允許用戶指定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(); 
} 
} 
+0

u能請幫助我,我使用相同的代碼什麼ü提及,但未能從tosdcard onButtonClick事件素材複製圖像u能幫幫我嗎? – Erum

+0

請參閱下面的接受答案,我已遵循,它工作正常。也沒有什麼可以說,除非訪問您的代碼是可用的。請將您的查詢作爲新問題發佈。 – Skynet

+0

我可以在聊天室與我討論我的代碼嗎?你有信譽問問題 – Erum

回答

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" /> 
相關問題