Android外部存儲示例含代碼

Android外部存儲示例

與內部存儲一樣,我們可以在設備外部存儲器(如sd卡)中保存或讀取數據。愛掏網 - it200.com FileInputStream和FileOutputStream類用于讀寫文件中的數據。愛掏網 - it200.com

在Android外部存儲中讀寫數據的示例

activity_main.xml

從調色板中拖動2個編輯框、2個文本視圖和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.javatpoint.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 />  
    </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" />  

    <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  
        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:" />  

    <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  
        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" />  
</RelativeLayout>  

為外部存儲提供權限

您需要提供WRITE_EXTERNAL_STORAGE權限。愛掏網 - it200.com

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Activity_Manifest.xml

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.javatpoint.com.externalstorage">  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
    <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" />  

                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  

</manifest>  

Activity類

讓我們編寫代碼來從Android外部存儲中寫入和讀取數據。愛掏網 - it200.com

package example.javatpoint.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);

        //Performing action on save button
        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();}
            }
        });

        //Performing action on Read Button
        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();
            }
        });
    }
}

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 国产不卡视频在线观看| 天天夜天干天天爽| 免费看黄色网页| A级毛片无码免费真人| 波多野结衣作品大全| 国产超碰人人模人人爽人人喊| 亚洲国产精品综合久久2007| 国产你懂的在线| 无码人妻一区二区三区免费视频| 午夜丰满少妇性开放视频| 99热在线精品播放| 欧美FREESEX潮喷| 国产三级在线电影| loveme枫と铃樱花动漫| 欧美性大战xxxxx久久久| 国产在线98福利播放视频免费 | 80s国产成年女人毛片| 极品丝袜老师h系列全文阅读 | 久久精品无码一区二区三区 | 日本国产在线视频| 拨牐拨牐x8免费| 亚洲色成人网站WWW永久| 五月天国产视频| 成全高清视频免费观看| 亚洲精品乱码久久久久久| 91抖音在线观看| 婷婷五月综合激情| 亚洲人成777在线播放| 美女的尿口视频网站| 国内免费在线视频| 久久人妻av无码中文专区| 番肉动漫无修在线观看网站| 国产精品扒开腿做爽爽爽的视频| 久久亚洲AV无码精品色午夜麻豆| 男人j进女人p免费视频不要下载的| 国产精品亚洲一区二区无码| 中文字幕在线免费视频| 欧美日韩亚洲中文字幕二区| 国产三级精品在线观看| 97日日碰曰曰摸日日澡| 日日麻批免费40分钟无码|