首先在AndroidManifest.xml添加权限:
<!-- SD卡操作权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <TableLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.tecapogee.www.lowpowerwireless.MySQLExportActivity"> <!--第一行--> <TableRow android:id="@+id/my_aqlexportTableRow1" android:layout_height="wrap_content" android:layout_width="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent"> <TextView android:id="@+id/my_aqlexportTableRow1_TextView" android:gravity="left" android:layout_width="match_parent" android:layout_height="wrap_content" > </TextView> </LinearLayout> </TableRow> <!--第二行--> <TableRow android:id="@+id/my_aqlexportTableRow2" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="1"> <LinearLayout android:layout_weight="1" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/my_aqlexport_ListView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout> </TableRow> <!--第三行--> <TableRow android:id="@+id/my_aqlexportTableRow3" android:layout_height="match_parent" android:layout_width="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent"> <ImageButton android:background="#00000000" android:src="@drawable/mycancle" android:id="@+id/my_aqlexportTableRow3_ButtonCancel" android:layout_weight="1" android:onClick="my_mainButton_Cancel" android:adjustViewBounds="true" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ImageButton> <ImageButton android:background="#00000000" android:src="@drawable/mycopy" android:id="@+id/my_aqlexportTableRow3_ButtonCopy" android:layout_weight="1" android:onClick="my_SQLExportButton_Save_onClick" android:adjustViewBounds="true" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ImageButton> </LinearLayout> </TableRow> </TableLayout>
java文件:
package com.tecapogee.www.lowpowerwireless; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.os.Process; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; public class MySQLExportActivity extends AppCompatActivity { protected ListView my_SQLExport_listview = null; protected TextView my_SQLExport_CurrentDirectory = null; private ImageButton myButtonSaveFile; public Handler my_SQLExportActivity = null; private String myStr_CurrentDirectory = "/"; private String myStr_RootDirectory = "/"; private String myStr_BackToLastDirectory = "/"; final int[] my_SQLExport_imageld=new int[]{R.drawable.file_root,R.drawable.file_back, R.drawable.file_open,R.drawable.file_file,R.drawable.file_db}; //导出数据库相关变量,开启一个线程保存,防止主UI不能响应 private Thread my_SQLExportActivity_Savethread=null; private boolean isSaving = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_sqlexport); //初始化变量 my_SQLExport_listview = (ListView)MySQLExportActivity.this.findViewById(R.id.my_aqlexport_ListView); my_SQLExport_CurrentDirectory=(TextView) MySQLExportActivity.this.findViewById(R.id.my_aqlexportTableRow1_TextView); myStr_RootDirectory = Environment.getRootDirectory().getPath().toString(); myButtonSaveFile = (ImageButton) MySQLExportActivity.this.findViewById(R.id.my_aqlexportTableRow3_ButtonCopy); my_SQLExport_RefreshList("/"); my_SQLExportActivity = new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what){ case 1: break; case 2: my_SQLExport_RefreshList(myStr_CurrentDirectory); break; } Toast.makeText(MySQLExportActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show(); super.handleMessage(msg); } }; //List控件的监听事件 my_SQLExport_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub HashMap<String, String> my_map = (HashMap<String, String>) my_SQLExport_listview.getItemAtPosition(arg2); String title = my_map.get("title"); /*Toast.makeText(MySQLExportActivity.this,content + "",Toast.LENGTH_SHORT).show();*/ switch (arg2){ case 0: my_SQLExport_RefreshList(myStr_RootDirectory); break; case 1: my_SQLExport_RefreshList(myStr_BackToLastDirectory); break; default: if(title.contains(".")){ Toast.makeText(MySQLExportActivity.this,"这是一个文件,无法打开!!",Toast.LENGTH_SHORT).show(); }else { if (!myStr_CurrentDirectory.equals("/")) { my_SQLExport_RefreshList(myStr_CurrentDirectory + "/" + title); } else { my_SQLExport_RefreshList(myStr_CurrentDirectory + title); } } break; } } }); }//END create @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if(keyCode == KeyEvent.KEYCODE_BACK){ if(isSaving){ Toast.makeText(MySQLExportActivity.this,"正在保存,请稍等。。。。。。",Toast.LENGTH_SHORT).show(); return true; } } super.onKeyDown(keyCode, event); return true; } public void my_SQLExportButton_Save_onClick(View my_onClick){ if(isSaving){ Toast.makeText(MySQLExportActivity.this,"正在保存,请稍等。。。。。。",Toast.LENGTH_SHORT).show(); return; } isSaving = true; /*myButtonSaveFile.setEnabled(false);*/ my_SQLExportActivity_Savethread = new Thread(new Runnable() { @Override public void run() { String strResult = null; try { File testFileCanWrite = new File(myStr_CurrentDirectory); if (!testFileCanWrite.canWrite()) { strResult = "没有操作权限!!"; Message SQLExportActivity_Savethread1 = new Message(); SQLExportActivity_Savethread1.what = 1; SQLExportActivity_Savethread1.obj = strResult; my_SQLExportActivity.sendMessage(SQLExportActivity_Savethread1); /*Toast.makeText(MySQLExportActivity.this, "没有操作权限!!", Toast.LENGTH_SHORT).show();*/ testFileCanWrite = null; return; } File oldfile = new File("/data/data/你的包名/databases/" + "你的文件名"); if (oldfile.exists()) { /*Toast.makeText(MySQLExportActivity.this, "正在复制文件!!", Toast.LENGTH_SHORT).show();*/ strResult = "正在复制文件!!"; Message SQLExportActivity_Savethread2 = new Message(); SQLExportActivity_Savethread2.what = 1; SQLExportActivity_Savethread2.obj = strResult; my_SQLExportActivity.sendMessage(SQLExportActivity_Savethread2); InputStream inputStream = new FileInputStream(oldfile); String saveFilePath = ""; String saveFlieNema = "LW"; Calendar sys_timer = Calendar.getInstance(); saveFlieNema += sys_timer.get(Calendar.YEAR) + "-" + sys_timer.get(Calendar.MONTH) + "-" + sys_timer.get(Calendar.DATE) + " " + sys_timer.get(Calendar.HOUR_OF_DAY) + "-" + sys_timer.get(Calendar.MINUTE) + "-" + sys_timer.get(Calendar.SECOND) + ".db3"; saveFilePath = myStr_CurrentDirectory; if (!myStr_CurrentDirectory.equals("/")) { saveFilePath += "/"; } saveFilePath += saveFlieNema; File mySaveFile = new File(saveFilePath); if (mySaveFile.exists()) { mySaveFile.delete(); } mySaveFile.createNewFile(); FileOutputStream fs = new FileOutputStream(mySaveFile, true); byte[] inByte = new byte[1024 * 5]; int len; while ((len = inputStream.read(inByte)) != -1) { fs.write(inByte, 0, len); } inputStream.close(); fs.close(); oldfile = null; mySaveFile = null; /*Toast.makeText(MySQLExportActivity.this, "复制文件成功!!", Toast.LENGTH_SHORT).show();*/ strResult = "复制文件成功!!"; Message SQLExportActivity_Savethread3 = new Message(); SQLExportActivity_Savethread3.what = 2; SQLExportActivity_Savethread3.obj = strResult; my_SQLExportActivity.sendMessage(SQLExportActivity_Savethread3); } else { /*Toast.makeText(MySQLExportActivity.this, "源数据库文件不存在!!", Toast.LENGTH_SHORT).show();*/ strResult = "源数据库文件不存在!!"; Message SQLExportActivity_Savethread4 = new Message(); SQLExportActivity_Savethread4.what = 1; SQLExportActivity_Savethread4.obj = strResult; my_SQLExportActivity.sendMessage(SQLExportActivity_Savethread4); } }catch (Exception e) { e.printStackTrace(); /*Toast.makeText(MySQLExportActivity.this, "复制文件出错!!", Toast.LENGTH_SHORT).show();*/ strResult = "复制文件出错!!"; Message SQLExportActivity_Savethread5 = new Message(); SQLExportActivity_Savethread5.what = 1; SQLExportActivity_Savethread5.obj = strResult; my_SQLExportActivity.sendMessage(SQLExportActivity_Savethread5); }finally { strResult = null; isSaving = false; } } }); my_SQLExportActivity_Savethread.start(); } public void my_SQLExport_RefreshList(String mDirectory){ File myFile = new File(mDirectory); if(mDirectory.equals("/")){ myStr_CurrentDirectory = myFile.getAbsolutePath(); myStr_BackToLastDirectory = myStr_CurrentDirectory; myStr_RootDirectory = myStr_CurrentDirectory; }else { myStr_BackToLastDirectory = myFile.getParentFile().getAbsolutePath().toString(); myStr_CurrentDirectory = myFile.getAbsolutePath(); } my_SQLExport_CurrentDirectory.setText(myStr_CurrentDirectory); ArrayList<File> myFilesList = new ArrayList<File>(); File[] filterFiles = myFile.listFiles(); List<Map<String,Object>> my_list = new ArrayList<Map<String, Object>>(); if((null != filterFiles) && (filterFiles.length>0)){ for (File file : filterFiles){ myFilesList.add(file); } } Map<String,Object> map = null; //添加返回根目录 map=new HashMap<String,Object>(); map.put("image",my_SQLExport_imageld[0]); map.put("title","返回根目录"); map.put("permission",""); my_list.add(map); map = null; //添加返回上级目录 map=new HashMap<String,Object>(); map.put("image",my_SQLExport_imageld[1]); map.put("title","返回上级目录"); map.put("permission",""); my_list.add(map); map = null; for(int i=0;i<myFilesList.size();i++){ map=new HashMap<String,Object>(); if(myFilesList.get(i).getName().toString().contains(".db")){ map.put("image",my_SQLExport_imageld[4]); }else if(myFilesList.get(i).getName().toString().contains(".")){ map.put("image",my_SQLExport_imageld[3]); }else { map.put("image",my_SQLExport_imageld[2]); } map.put("title",myFilesList.get(i).getName().toString()); if(myFilesList.get(i).canWrite() && myFilesList.get(i).canRead()){ map.put("permission","可读可写"); }else if((!myFilesList.get(i).canWrite()) && myFilesList.get(i).canRead()){ map.put("permission","只读"); }else if(myFilesList.get(i).canWrite() && (!myFilesList.get(i).canRead())){ map.put("permission","只写"); }else { map.put("permission","无权限"); } my_list.add(map); map=null; } SimpleAdapter adapter=new SimpleAdapter(MySQLExportActivity.this,my_list,R.layout.my_sqlexport_list_items, new String[]{"title","permission","image"}, new int[]{R.id.my_sqlexport_list_items_Name, R.id.my_sqlexport_list_items_Permissions, R.id.my_sqlexport_list_items_ImageView}); my_SQLExport_listview.setAdapter(adapter); myFile = null; } //取消按钮 public void my_mainButton_Cancel(View my_onClick) { if(isSaving){ Toast.makeText(MySQLExportActivity.this,"正在保存,请稍等。。。。。。",Toast.LENGTH_SHORT).show(); return; } this.finish(); } }
列表xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_sqlexport_list_items_list_items_LinearLayout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/my_sqlexport_list_items_ImageView" android:adjustViewBounds="true" android:maxHeight="72px" android:maxWidth="72px" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <TextView android:id="@+id/my_sqlexport_list_items_Name" android:gravity="left" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/my_sqlexport_list_items_Permissions" android:gravity="right" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout>