saving file in internal storage android

前端 未结 2 2061
天命终不由人
天命终不由人 2020-12-01 11:11

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.

<
相关标签:
2条回答
  • 2020-12-01 11:46

    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();
        }
    
    0 讨论(0)
  • 2020-12-01 11:47

    I had "Permission denied" issue with Samsung Galaxy S7 even though I had given read and write permissions in manifest file. I solved it by going to phone settings >> application >> [my app] and allowed "Storage" under permission. Worked fine after that.

    0 讨论(0)
提交回复
热议问题