我有一个应用程序和相关数据,我可以将这些数据从我的 Assest 文件夹复制到 SD卡,这是完美的。但我希望改进此代码并允许用户在SD卡上指定位置,例如她想要存储数据的位置。这是我的代码,请提出修改建议。 (我想生成类似的弹出窗口,当从互联网上下载文件时弹出窗口要求用户指定下载位置,我感到困惑的是我是否需要自定义弹出这个弹出窗口,或者是inbuilt)以下是我的代码:
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 :(得分:2)
基于AlertDialog选择目录或文件的Android对话框将在这种情况下为您提供帮助。
http://www.ulduzsoft.com/2012/07/android-dialog-to-choose-a-directory-or-file-based-on-alertdialog/
答案 1 :(得分:0)
以下是我准备的最终代码(来自Google的有价值的输入):
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" />