在内部存储的Android保存文件(saving file in internal storage

2019-06-18 17:40发布

我是新到Android,和我遇到的时候我试图将文件保存到内部存储的问题,新的例子适用于我的SDK,但我的手机上不能正常工作。

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

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

谢谢。

    @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);
                }
            });
            }

在我的Android manifiest我拥有的权限

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

Answer 1:

我不太清楚你指的是哪个例子,但我有两个工作在这里的样本,其中他们至少应该满足您的需求。 我测试这些上的X10运行生成数2.1.A.0.435,一个的XperiaŤ运行生成数7.0.A.1.303和一个Nexus S的运行生成数JZO54K

实施例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();
    }

实施例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();
    }


Answer 2:

我曾在清单文件“权限不足”的问题与三星Galaxy S7,即使我给了读取和写入权限。 我的许可范围内将手机设置>>应用>> [我的应用程序],并允许“存储”解决了这个问题。 之后的正常工作。



文章来源: saving file in internal storage android