【问题标题】:How to make a button write a file to sdcard?如何让按钮将文件写入 sdcard?
【发布时间】:2012-04-15 02:41:35
【问题描述】:

在我的应用程序中,我想要一个按钮,按下该按钮会将存储在我的应用程序原始文件夹中的文件复制到 sdcard/Android/data... 覆盖已经存在的现有文件。

这是我目前所拥有的。例如,我的原始文件夹中的文件称为brawler.dat

我不是要求任何人编写整个代码,但这肯定是一个奖励。

我主要需要有人为我指明正确的方向。

我可以创建按钮来访问 URL 等...但我已经准备好进入下一个级别了,我感觉。

main.xml

 rLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Overwrite File" />

FreelineActivity.java

     package my.freeline.conquest;

import android.app.Activity;
import android.os.Bundle;

public class FreelineActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

【问题讨论】:

    标签: java android android-sdcard overwrite copying


    【解决方案1】:

    你可以做如下简单的调用copyRawFile()。关于存储的更多细节参考http://developer.android.com/guide/topics/data/data-storage.html

        private void copyRawFile() {
    
    
                    InputStream in = null;
                    OutputStream out = null;
        String filename="myFile"; //sd card file name           
        try {
    //Provide the id of raw file to the openRawResource() method
                      in = getResources().openRawResource(R.raw.brawler);
                      out = new FileOutputStream("/sdcard/" + filename);
                      copyFile(in, out);
                      in.close();
                      in = null;
                      out.flush();
                      out.close();
                      out = null;
                    } catch(Exception e) {
                        Log.e("tag", e.getMessage());
                    }       
    
            }
            private void copyFile(InputStream in, OutputStream out) throws IOException {
                byte[] buffer = new byte[1024];
                int read;
                while((read = in.read(buffer)) != -1){
                  out.write(buffer, 0, read);
                }
            }
    

    【讨论】:

      【解决方案2】:

      以这种方式获取原始资源输入流:

      // in your activity in `onClick` event of the button:
      InputStream is = getResources().openRawResource(R.raw.yourResourceName);
      

      然后将其读取到缓冲区并写入到文件输出流中:

      OutputStream os = new FileOutputStream("real/path/name"); // you'll need WRITE_EXTERNAL_STORAGE permission for writing in external storage
      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = is.read(buffer, 0, buffer.length)) > 0) {
        os.write(buffer, 0, size);
      }
      is.close();
      os.close();
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-31
        相关资源
        最近更新 更多