如何将文件保存到SD卡[重复]

Posted

技术标签:

【中文标题】如何将文件保存到SD卡[重复]【英文标题】:How to save file to sd card [duplicate] 【发布时间】:2011-07-13 04:52:15 【问题描述】:

我是android和java编程的新手,谁能告诉我如何保存文件,我有以下代码:

InputStream is = new FileInputStream( location + "/zipSample.zip");
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));

try 
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int count;
        while ((count = zis.read(buffer)) != -1) 
            baos.write(buffer, 0, count);
        

        String filename = ze.getName();
        byte[] bytes = baos.toByteArray();

        // Do something with 'filename' and 'bytes' ...
        // How do I save to sdcard?
    
 finally 
    zis.close();

非常感谢您的帮助。

【问题讨论】:

【参考方案1】:

FileDemo2.java:

package com.javasamples;

import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class FileDemo2 extends Activity 
    // GUI controls
    EditText txtData;
    Button btnWriteSDFile;
    Button btnReadSDFile;
    Button btnClearScreen;
    Button btnClose;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // bind GUI elements with local controls
    txtData = (EditText) findViewById(R.id.txtData);
    txtData.setHint("Enter some lines of data here...");

    btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
    btnWriteSDFile.setOnClickListener(new OnClickListener() 

    public void onClick(View v) 
        // write on SD card file data in the text box
        try 
            File myFile = new File("/sdcard/mysdfile.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = 
                                    new OutputStreamWriter(fOut);
            myOutWriter.append(txtData.getText());
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(),
                    "Done writing SD 'mysdfile.txt'",
                    Toast.LENGTH_SHORT).show();
         catch (Exception e) 
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        
    // onClick
    ); // btnWriteSDFile

        btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
        btnReadSDFile.setOnClickListener(new OnClickListener() 

        public void onClick(View v) 
            // write on SD card file data in the text box
        try 
            File myFile = new File("/sdcard/mysdfile.txt");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) 
                aBuffer += aDataRow + "\n";
            
            txtData.setText(aBuffer);
            myReader.close();
            Toast.makeText(getBaseContext(),
                    "Done reading SD 'mysdfile.txt'",
                    Toast.LENGTH_SHORT).show();
         catch (Exception e) 
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        
        // onClick
        ); // btnReadSDFile

        btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
        btnClearScreen.setOnClickListener(new OnClickListener() 

            public void onClick(View v) 
                // clear text box
                txtData.setText("");
            
        ); // btnClearScreen

        btnClose = (Button) findViewById(R.id.btnClose);
        btnClose.setOnClickListener(new OnClickListener() 

            public void onClick(View v) 
                // clear text box
                finish();
            
        ); // btnClose

    // onCreate

// AndSDcard

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget28"
android:layout_
android:layout_
android:background="#ff0000ff"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<EditText
android:id="@+id/txtData"
android:layout_
android:layout_
android:textSize="18sp"/>

<Button
android:id="@+id/btnWriteSDFile"
android:layout_
android:layout_
android:text="1. Write SD File"  />

<Button
android:id="@+id/btnClearScreen"
android:layout_
android:layout_
android:text="2. Clear Screen"/>

<Button
android:id="@+id/btnReadSDFile"
android:layout_
android:layout_
android:text="3. Read SD File"  />

<Button
android:id="@+id/btnClose"
android:layout_
android:layout_
android:text="4. Close" />

</LinearLayout>

【讨论】:

这是一个很好的答案,谢谢!我会在保存中添加一个子文件夹: File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyFolder/"); storagePath.mkdirs(); File myFile = new File(storagePath, "mysdfile.txt");

以上是关于如何将文件保存到SD卡[重复]的主要内容,如果未能解决你的问题,请参考以下文章

flutter 将assets文件保存到sd卡

将图像、音频和视频保存到 SD 卡

自动将视频文件保存到 Android SD 卡

将文本文件保存到 sd 卡

将位图从 arraylist 保存到 SD 卡 - 保存的文件不可读

如何将可绘制资源中的图像保存到 SD 卡?