如何将文件保存到SD卡

我是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(); } 

非常感谢您的帮助。

如果您需要更多帮助,请发布好看的答案: 向SD卡写入文件

javafile。

 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 xmlfile.