1

我无法将文件从资产文件夹复制到 SD 卡。我怎样才能做到这一点?或者有没有办法在安装应用程序期间将文件从资产或任何其他文件夹复制到 sd 卡?这是我的代码:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CopyAssets();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

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/" + 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); 
    } 
}


}
4

3 回答 3

3

你不应该硬编码存储目录。使用Environment.getExternalStorageDirectory()

String destFile = Environment.getExternalStorageDirectory().toString().concat("/ans");
try {

        File f2 = new File(destFile);
        InputStream in = getAssets().open("try.xml");
        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out
                .println(ex.getMessage() + " in the specified directory.");

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

在清单中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-07-08T08:30:34.360 回答
2

要在 sdcard 中写入文件,您必须授予清单上的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你是否?

于 2012-07-08T08:24:32.430 回答
0

将此代码添加到您的 onCreate。因此,您的应用程序在安装后第一次启动时,将资产复制到 SD 卡的功能,然后在您再次进入应用程序时不再调用它。

        SharedPreferences score = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor score_inc = score.edit();
    int counter = score.getInt("counter",0);
    float font_size = score.getFloat("font_size",20.0f);
    if(counter==0)
    {
        //Put your function to copy files here
        score_inc.putInt("counter", ++counter);
        score_inc.commit();
        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
    }
于 2013-10-16T13:32:52.960 回答