android将raw或者assets中的文件复制到手机中

来源:互联网 发布:黑色星期天 知乎 编辑:程序博客网 时间:2024/04/29 15:42

每个应用程序可能都会有程序本身携带的数据,

有的是txt文本,有的是db数据库,有的是具体的一些文件,那么如何从程序中得到文件呢

方法一:

该方式是将数据库db文件放到res/raw中,在获取的时候将文件写入到手机的sd卡中,

建立数据库的时候可以调用

SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);得到数据库
具体代码如下:

public class ParentActivity extends Activity
{
 protected final String DATABASE_PATH = android.os.Environment
 .getExternalStorageDirectory().getAbsolutePath()+ "/dictionary";
 protected final String DATABASE_FILENAME = "dictionary.db";
 protected SQLiteDatabase database;
 protected SQLiteDatabase openDatabase()
 {
  try
  {
   String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
   File dir = new File(DATABASE_PATH);
   if (!dir.exists())
    dir.mkdir();
   if (!(new File(databaseFilename)).exists())
   {
    Log.d("aa", "bbb");
    InputStream is = getResources().openRawResource(
      R.raw.dictionary);
    FileOutputStream fos = new FileOutputStream(databaseFilename);
    byte[] buffer = new byte[8192];
    int count = 0;
    while ((count = is.read(buffer)) > 0)
    {
     fos.write(buffer, 0, count);
    }

    fos.close();
    is.close();
   }
   SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
     databaseFilename, null);
   return database;
  }
  catch (Exception e)
  {
  }
  return null;
 }

}

方法二:

文件被放在了assets中,然后加载到手机内存rom中的data中


 

public class DB_Copy {
 private Context mContext;
 private String mDB_PATH;
 private String mDATABASE_NAME = "";

 public DB_Copy(Context context, String dbFileName) {
  mContext = context;
  mDB_PATH = "/data/data/" + mContext.getPackageName() + "/databases/";
  mDATABASE_NAME = dbFileName;
 }

 /**
  * 判断数据库文件是否存在
  *
  * @return
  */
 public boolean isDataBaseExist() {
  File dbFile = new File(mDB_PATH + mDATABASE_NAME);
  return dbFile.exists();
 }
 
 /**
  * 删除数据库文件
  */
 public void delteFile(){
  String collectName = mDB_PATH + mDATABASE_NAME;
  File collectFile = new File(collectName);
  collectFile.delete();
 }

 /**
  * 存储数据库文件
  */
 public voidcopyDataBase() {
  try {
   InputStream myInput = mContext.getAssets().open("databases/" + mDATABASE_NAME);
   String outFileName = mDB_PATH + mDATABASE_NAME;
   File file = new File(outFileName);
   if(file.exists()){
    file.delete();
   }
   file.createNewFile();
   OutputStream myOutput = new FileOutputStream(file);   
   byte[] buffer = new byte[1024];
   int length;
   while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
   }
   // Close the streams
   myOutput.flush();
   myOutput.close();
   myInput.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 /**
  * 存储数据库文件
  */
 public void copyDataBaseToSdcard() {
  try {
//   InputStream myInput = mContext.getAssets().open("databases/" + mDATABASE_NAME);
   String inputFileName = mDB_PATH + mDATABASE_NAME;   
   File inputFile = new File(inputFileName); 
   if(inputFile == null || !inputFile.exists()){
    return;
   }
   InputStream myInput = new FileInputStream(inputFile);
   File file = Sdcard.getSdcard("lyb" + mDATABASE_NAME , "");
   if(file.exists()){
    file.delete();
   }
   file.createNewFile();
   OutputStream myOutput = new FileOutputStream(file);
   byte[] buffer = new byte[1024];
   int length;
   while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
   }
   // Close the streams
   myOutput.flush();
   myOutput.close();
   myInput.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

如果是要拷贝其中的书籍到sd卡中的话

private void copyAssetBooksIntoSdcard() {
  String[] locales;
  AssetManager manager = this.getAssets();
  final String path = "ekoudai" + File.separator + "collect";
  try {
   locales = manager.list("");
   FileUtils fu = new FileUtils();
   for (String s : locales) {
    if (s.endsWith(".ekd")) {
     InputStream is = manager.open(s);
     fu.write2SDfromInput(path, s, is);
    }
   }
   locales = null;
   fu = null;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

public File write2SDfromInput(String path,String fileName,final InputStream input)
 {
  File file = null;
  OutputStream output = null;
  int byteread = 0; // 读取的字节数
  try {
   creatSDDir(path);
   file=creatSDFile(fileName, path);
   output = new FileOutputStream(file);
   byte buffer [] = new byte[1024*4];
  
    while ((byteread = input.read(buffer)) != -1) {
     output.write(buffer, 0, byteread);
    }
    output.flush();
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally{
   try{
    input.close();
    output.close();
   }
   catch(Exception e){
    e.printStackTrace();
   }
  }
  return file;
 }

public File creatSDFile(String fileName, String dir) throws IOException
 {
  File file = new File(sd_path + dir + File.separator + fileName);
  file.createNewFile();
  return file;
  
 }

/**
  * 在SD卡上创建目录
  *
  * @param dirName
  */
 public File creatSDDir(String dirName)
 {
  
  File dirFile = new File(sd_path + dirName);
  dirFile.mkdirs();
  //System.out.println("creatSDDir:"+dirFile.mkdirs());
  return dirFile;
  
 }


原创粉丝点击