与内部存储一样,无涯教程能够从设备外部存储器(如SDCard)中浏览或读取数据。 FileInputStream和FileOutputStream类用于读取和写入文件。
从面板上拖动2个edittexts,2个textviews和2个按钮,现在activity_main.xml文件将如下所示:
<!--?xml version="1.0" encoding="utf-8"?--> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="example.learnfk.com.externalstorage.MainActivity"> <edittext android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_marginright="20dp" android:layout_margintop="24dp" android:ems="10"> <requestfocus></requestfocus> </edittext> <edittext android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/editText1" android:layout_below="@+id/editText1" android:layout_margintop="24dp" android:ems="10"></edittext> <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/editText1" android:layout_alignbottom="@+id/editText1" android:layout_alignparentleft="true" android:text="File Name:"></textview> <textview android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/editText2" android:layout_alignbottom="@+id/editText2" android:layout_alignparentleft="true" android:text="Data:"></textview> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginleft="70dp" android:layout_margintop="16dp" android:text="save"></button> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/button1" android:layout_alignbottom="@+id/button1" android:layout_torightof="@+id/button1" android:text="read"></button> </relativelayout>
您需要提供write_external_storage权限。
链接:https://www.learnfk.comhttps://www.learnfk.com/android/android-external-storage-example.html
来源:LearnFk无涯教程网
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--?xml version="1.0" encoding="utf-8"?--> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.learnfk.com.externalstorage"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="android.intent.category.LAUNCHER"></category> </intent-filter> </activity> </application> </manifest>
让无涯教程编写代码来编写和读取来自Android外部存储的数据。
package example.learnfk.com.externalstorage; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class MainActivity extends AppCompatActivity { EditText editTextFileName,editTextData; Button saveButton,readButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextFileName=findViewById(R.id.editText1); editTextData=findViewById(R.id.editText2); saveButton=findViewById(R.id.button1); readButton=findViewById(R.id.button2); //对保存按钮执行操作 saveButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { String filename=editTextFileName.getText().toString(); String data=editTextData.getText().toString(); FileOutputStream fos; try { File myFile = new File("/sdcard/"+filename); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(data); myOutWriter.close(); fOut.close(); Toast.makeText(getApplicationContext(),filename + "saved",Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} } }); //对读取按钮执行操作 readButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { String filename=editTextFileName.getText().toString(); StringBuffer stringBuffer = new StringBuffer(); String aDataRow = ""; String aBuffer = ""; try { File myFile = new File("/sdcard/"+filename); FileInputStream fIn = new FileInputStream(myFile); BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn)); while ((aDataRow = myReader.readLine()) != null) { aBuffer += aDataRow + "\n"; } myReader.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show(); } }); } }
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)