android之sd卡操作

本文探讨了在Android应用中文件保存时遇到的问题,重点解释了由于活动组件生命周期的原因导致文件保存失败的情况,并提供了通过添加关闭按钮来解决此问题的方法。同时介绍了如何在资产目录中读取文件并将其保存到SD卡上,以及如何处理文件保存过程中的异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主显示布局以及代码:

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下看,具体截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值