Android studio2.3.3 - 文件路径

时间:2017-07-30 11:40:03

标签: android

我在android studio中创建了一个文件,里面有一些文字。但是我无法指出我的文件在模拟器的文件浏览器中的实际位置。确认已创建文件。有些人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

private static final int REQUEST_ID_READ_PERMISSION = 100;
private static final int REQUEST_ID_WRITE_PERMISSION = 200;

private final String fileName = "note.txt";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.textView);

    saveButton = (Button) findViewById(R.id.button_save);
    readButton = (Button) findViewById(R.id.button_read);
    listButton = (Button) findViewById(R.id.button_list);

    saveButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            askPermissionAndWriteFile();
        }

    });


    readButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            askPermissionAndReadFile();
        }

    });

    listButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            listExternalStorages();
        }

    });
}

private void askPermissionAndWriteFile() {
    boolean canWrite = this.askPermission(REQUEST_ID_WRITE_PERMISSION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    //
    if (canWrite) {
        this.writeFile();
    }
}

private void askPermissionAndReadFile() {
    boolean canRead = this.askPermission(REQUEST_ID_READ_PERMISSION,
            Manifest.permission.READ_EXTERNAL_STORAGE);
    //
    if (canRead) {
        this.readFile();
    }
}

// With Android Level >= 23, you have to ask the user
// for permission with device (For example read/write data on the device).
private boolean askPermission(int requestId, String permissionName) {
    if (android.os.Build.VERSION.SDK_INT >= 23) {

        // Check if we have permission
        int permission = ActivityCompat.checkSelfPermission(this, permissionName);


        if (permission != PackageManager.PERMISSION_GRANTED) {
            // If don't have permission so prompt the user.
            this.requestPermissions(
                    new String[]{permissionName},
                    requestId
            );
            return false;
        }
    }
    return true;
}


// When you have the request results
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    //
    // Note: If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0) {
        switch (requestCode) {
            case REQUEST_ID_READ_PERMISSION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readFile();
                }
            }
            case REQUEST_ID_WRITE_PERMISSION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    writeFile();
                }
            }
        }
    } else {
        Toast.makeText(getApplicationContext(), "Permission Cancelled!", Toast.LENGTH_SHORT).show();
    }
}


private void writeFile() {

    File extStore = Environment.getExternalStorageDirectory();
    // ==> /storage/emulated/0/note.txt
    String path = extStore.getAbsolutePath() + "/" + fileName;
    Log.i("ExternalStorageDemo", "Save to: " + path);

    String data = editText.getText().toString();

    try {
        File myFile = new File(path);
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(data);
        myOutWriter.close();
        fOut.close();

        Toast.makeText(getApplicationContext(), fileName + " saved", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void readFile() {

    File extStore = Environment.getExternalStorageDirectory();
    // ==> /storage/emulated/0/note.txt
    String path = extStore.getAbsolutePath() + "/" + fileName;
    Log.i("ExternalStorageDemo", "Read file: " + path);

    String s = "";
    String fileContent = "";
    try {
        File myFile = new File(path);
        FileInputStream fIn = new FileInputStream(myFile);
        BufferedReader myReader = new BufferedReader(
                new InputStreamReader(fIn));

        while ((s = myReader.readLine()) != null) {
            fileContent += s + "\n";
        }
        myReader.close();

        this.textView.setText(fileContent);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Toast.makeText(getApplicationContext(), fileContent, Toast.LENGTH_LONG).show();
}