主显示布局以及代码:
activity_main.xml:
(总结:刚才总是显示不出文件写入,因为刚才那个打开的view并没有关闭,所以通过Eclipse重新发布,或者debug都没用的,所以我加了个关闭按钮,通过关闭按钮就可以重新发布,或者debug,本质原因:activities的生命周期就是窗口没有关闭的话,只会调用onRestart()再到onStart(),所以并不会调用onCreate()方法)
<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: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.litsoft.day0606.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv"
android:text="关闭窗口"/>
</RelativeLayout>
java代码:
package com.litsoft.day0606;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private final static String FILE_NAME = "sxt_logo.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saveSdcard();
setListener();
}
private void saveSdcard() {
// TODO Auto-generated method stub
InputStream in = null;
OutputStream out = null;
try {
in = getAssets().open(FILE_NAME);
//打开assets目录下的文件,需要权限,也就是菜单列表配置的读取外部文件的权限
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//得到android预定义的sd卡中的pictures目录,需要权限,也就是菜单列表配置的写外部文件的权限
File file = new File(dir,FILE_NAME);
out = new FileOutputStream(file);
byte[] temp = new byte[1024*8];
while(in.read(temp)!=-1){
out.write(temp);
}
out.flush();
Log.i("main", "文件保存完毕");
Toast.makeText(this, "文件保存完毕", 2000).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(in !=null){
in.close();
}
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.btClose).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
finish();
}
});
}
}
列表菜单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.litsoft.day0606"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
效果是:
具体路径是storage--sdcard--Pictures,在File
Explorer下看,具体截图: