在内部存储的android文件保存 [英] saving file in internal storage android

查看:300
本文介绍了在内部存储的android文件保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人,而且我有,当我试图将文件保存到内部存储的问题,新的例子可以在我的SDK,但不能在我的手机正常工作。

我试图运行德例如在索尼爱立信Xperia,与Android 2.1的方式......在log.i - 给我的下一行:

  /data/data/com.example.key/files/text/(my_title)
 

感谢。

  @覆盖
        保护无效的onCreate(包savedInstanceState){
            // TODO自动生成方法存根
            super.onCreate(savedInstanceState);

            的setContentView(R.layout.new_text);

            文件=(EditText上)findViewById(R.id.title_new);
            进入=(EditText上)findViewById(R.id.entry_new);

            BTN =(按钮)findViewById(R.id.save_new);
            btn.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){

                    文件MYDIR = getFilesDir();


                    NEWFILENAME = file.getText()的toString()。
                    如果(NEWFILENAME.contentEquals()){
                        NEWFILENAME =无题;
                    }
                    。NEWENTRY = entry.getText()的toString();

                    尝试 {

                        文件fil​​e_new =新的文件(MYDIR +/文/,NEWFILENAME);
                        file_new.createNewFile();

                        Log.i(文件,file_new.toString());

                        如果(file_new.mkdirs()){
                            FileOutputStream中FOS =新的FileOutputStream(file_new);

                            fos.write(NEWENTRY.getBytes());
                            fos.flush();
                            fos.close();
                        }

                    }赶上(FileNotFoundException异常E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }赶上(IOException异常E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }

                    意图textPass =新的意向书(com.example.TEXTMENU);
                    startActivity(textPass);
                }
            });

            }


 //这是创建......那么其他活动我读

            @覆盖
        保护无效的onCreate(包savedInstanceState){
            // TODO自动生成方法存根
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.text_menu);

            BTN =(按钮)findViewById(R.id.newNote);
            listfinal =(ListView控件)findViewById(R.id.listView);

            btn.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    意图textPass =新的意向书(com.example.TEXTNEW);
                    startActivity(textPass);

                }
            });

            listfinal.setOnItemClickListener(本);

            文件fil​​eWithinMyDir = getApplicationContext()getFilesDir()。


            loadbtn =(按钮)findViewById(R.id.loadList);

            loadbtn.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    文件MYDIR = getFilesDir();

                    文件DIR =新的文件(MYDIR +/文/);

                    的String []文件= dir.list();

                    //的String []文件= getApplicationContext()的fileList()。
                    名单<字符串>名单=新的ArrayList<字符串>();

                    的for(int i = 0; I< files.length;我++){
                        list.add(文件[I]);
                    }
                    ArrayAdapter<字符串> AD =新的ArrayAdapter<字符串>(TextMenu.this,android.R.layout.simple_list_item_1,
                                    android.R.id.text1,清单);

                    listfinal.setAdapter(广告);
                }
            });
            }
 

在我的Andr​​oid manifiest我拥有的权限

 <使用-SDK
                    安卓的minSdkVersion =5
                    机器人:targetSdkVersion =15/>

                <使用-权限的Andr​​oid:名称=android.hardware.camera/>
                <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
                <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
                <使用-权限的Andr​​oid:名称=android.permission.WRITE_INTERNAL_STORAG​​E/>
 

解决方案

我不太清楚你指哪个例子,但我有两个工作的样品在这里,其中至少一人应满足您的需要。 我在一个X10运行内部版本号2.1.A.0.435测试了这些,人们的Xperia牛逼运行的内部版本号7.0.A.1.303一歌Nexus S运行的内部版本号JZO54K

例1

 字符串文件名=MYFILE;
    字符串outputString =世界,你好!;

    尝试 {
        FileOutputStream中的OutputStream = openFileOutput(文件名,Context.MODE_PRIVATE);
        outputStream.write(outputString.getBytes());
        outputStream.close();
    }赶上(例外五){
        e.printStackTrace();
    }

    尝试 {
        的FileInputStream的InputStream = openFileInput(文件名);
        BufferedReader中R =新的BufferedReader(新的InputStreamReader(InputStream中));
        StringBuilder的总=新的StringBuilder();
        串线;
        而((行= r.readLine())!= NULL){
            total.append(线);
        }
        r.close();
        inputStream.close();
        Log.d(文件,文件内容:+总);
    }赶上(例外五){
        e.printStackTrace();
    }
 

例2

 字符串文件名=mysecondfile;
    字符串outputString =世界,你好!;
    文件MYDIR = getFilesDir();

    尝试 {
        文件secondFile =新的文件(MYDIR +/文/,文件名);
        如果(secondFile.getParentFile()。mkdirs()){
            secondFile.createNewFile();
            FileOutputStream中FOS =新的FileOutputStream(secondFile);

            fos.write(outputString.getBytes());
            fos.flush();
            fos.close();
        }
    }赶上(例外五){
        e.printStackTrace();
    }

    尝试 {
        文件secondInputFile =新的文件(MYDIR +/文/,文件名);
        InputStream的secondInputStream =新的BufferedInputStream(新的FileInputStream(secondInputFile));
        BufferedReader中R =新的BufferedReader(新的InputStreamReader(secondInputStream));
        StringBuilder的总=新的StringBuilder();
        串线;
        而((行= r.readLine())!= NULL){
            total.append(线);
        }
        r.close();
        secondInputStream.close();
        Log.d(文件,文件内容:+总);
    }赶上(例外五){
        e.printStackTrace();
    }
 

i'm new to android, and i'm having a problem when i'm trying to save a file into internal storage, the new example works on my sdk, but doesn't work on my phone.

I'm trying to run de example in a sony Ericsson xperia, with android 2.1 by the way... the log.i - gives me the next line:

/data/data/com.example.key/files/text/(my_title) 

Thanks.

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.new_text);

            file = (EditText) findViewById(R.id.title_new);
            entry = (EditText) findViewById(R.id.entry_new);

            btn = (Button) findViewById(R.id.save_new);
            btn.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {

                    File myDir = getFilesDir();


                    NEWFILENAME = file.getText().toString();
                    if (NEWFILENAME.contentEquals("")){
                        NEWFILENAME = "UNTITLED";
                    }
                    NEWENTRY = entry.getText().toString();

                    try {

                        File file_new = new File(myDir+"/text/", NEWFILENAME);
                        file_new.createNewFile();

                        Log.i("file", file_new.toString());

                        if (file_new.mkdirs()) {
                            FileOutputStream fos = new FileOutputStream(file_new);

                            fos.write(NEWENTRY.getBytes());
                            fos.flush();
                            fos.close();
                        }

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Intent textPass = new Intent("com.example.TEXTMENU");
                    startActivity(textPass);
                }
            });

            }


 //That's for creating... then in other activity i'm reading

            @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text_menu);

            btn = (Button) findViewById(R.id.newNote);
            listfinal = (ListView) findViewById(R.id.listView);

            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent textPass = new Intent("com.example.TEXTNEW");
                    startActivity(textPass);

                }
            });

            listfinal.setOnItemClickListener(this);

            File fileWithinMyDir = getApplicationContext().getFilesDir();


            loadbtn = (Button) findViewById(R.id.loadList);

            loadbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    File myDir = getFilesDir();

                    File dir = new File(myDir + "/text/");

                    String[] files = dir.list();

                    //String[] files = getApplicationContext().fileList();
                    List<String> list = new ArrayList<String>();

                    for (int i =0; i < files.length; i++){
                        list.add(files[i]);
                    }
                    ArrayAdapter<String> ad = new ArrayAdapter<String>(TextMenu.this,  android.R.layout.simple_list_item_1,
                                    android.R.id.text1, list);

                    listfinal.setAdapter(ad);
                }
            });
            }

in my android manifiest i have the permissions

              <uses-sdk
                    android:minSdkVersion="5"
                    android:targetSdkVersion="15" />

                <uses-permission android:name="android.hardware.camera" />
                <uses-permission android:name="android.permission.INTERNET" />
                <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

解决方案

I'm not too sure which example you are referring to but I have two working samples here of which at least one of them should suit your needs. I tested these on an X10 running Build number 2.1.A.0.435, one Xperia T running Build number 7.0.A.1.303 and one Nexus S running Build number JZO54K

Example 1

    String filename = "myfile";
    String outputString = "Hello world!";

    try {
        FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(outputString.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        FileInputStream inputStream = openFileInput(filename);
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        r.close();
        inputStream.close();
        Log.d("File", "File contents: " + total);
    } catch (Exception e) {
        e.printStackTrace();
    }

Example 2

    String filename = "mysecondfile";
    String outputString = "Hello world!";
    File myDir = getFilesDir();

    try {
        File secondFile = new File(myDir + "/text/", filename);
        if (secondFile.getParentFile().mkdirs()) {
            secondFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(secondFile);

            fos.write(outputString.getBytes());
            fos.flush();
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        File secondInputFile = new File(myDir + "/text/", filename);
        InputStream secondInputStream = new BufferedInputStream(new FileInputStream(secondInputFile));
        BufferedReader r = new BufferedReader(new InputStreamReader(secondInputStream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        r.close();
        secondInputStream.close();
        Log.d("File", "File contents: " + total);
    } catch (Exception e) {
        e.printStackTrace();
    }

这篇关于在内部存储的android文件保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆