Android获取assets文件夹中的数据并写入SD卡示例

2022-10-19,,,,

这篇文章主要介绍了Android获取assets文件夹中的数据写入SD卡示例,对初学Android开发的朋友来说是一个很实用的功能,需要的朋友可以参考下

本文示例主要实现了Android获取assets文件夹中的数据并将其写入到SD卡中,该程序实现的步骤主要为:首先读取assets文件夹中的数据库,再将其写入到SD存储卡中。

完整示例代码如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
/*将assets文件夹下的数据库写入SD卡中
 * @author Dave */
public class WriteToSD {
 private Context context;
 String filePath = android.os.Environment.getExternalStorageDirectory()+"/weather";
 public WriteToSD(Context context){
 this.context = context;
 if(!isExist()){
  write();
 }
 }
 private void write(){
 InputStream inputStream;
 try {
  inputStream = context.getResources().getAssets().open("addressId.db");
  File file = new File(filePath);
  if(!file.exists()){
  file.mkdirs();
  }
  FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/database.db");
  byte[] buffer = new byte[512];
  int count = 0;
  while((count = inputStream.read(buffer)) > 0){
  fileOutputStream.write(buffer, 0 ,count);
  }
  fileOutputStream.flush();
  fileOutputStream.close();
  inputStream.close();
  System.out.println("success");
 } catch (IOException e) {
  e.printStackTrace();
 }
 }
 private boolean isExist(){
 File file = new File(filePath + "/database.db");
 if(file.exists()){
  return true;
 }else{
  return false;
 }
 }
}

您可能感兴趣的文章:

  • Android实现复制Assets文件到SD卡
  • Android编程读取Assets所有文件(遍历每一个文件夹)并存入sdcard的方法
  • android读取Assets图片资源保存到SD卡实例
  • Android复制assets文件到SD卡

《Android获取assets文件夹中的数据并写入SD卡示例.doc》

下载本文的Word格式文档,以方便收藏与打印。