首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内存内部android中的Writer文件TXT

内存内部android中的Writer文件TXT
EN

Stack Overflow用户
提问于 2016-03-30 20:08:17
回答 1查看 112关注 0票数 0

我需要在智能手机的内存中写入文本文件,然后需要通过USB线将此.txt文件复制到您的计算机并访问他的内存(此复制过程将进行手动操作,需要找到此文件并了解将记录哪个内存位置)。

我正在使用下面的代码,它在我运行时没有显示任何错误,但我不知道此录制是否正确,我也不知道它在您的智能手机上应该是什么目录。

此函数是调用函数salvarInternalStorage的按钮

代码语言:javascript
复制
    findViewById(R.id.distance_demo_button).setOnClickListener(new View.OnClickListener() {
   @Override public void onClick(View v) {
     Trilateration tri = new Trilateration(v.getContext());
     try {
       tri.salvarInternalStorage("Trying to Save This text Example");
     } catch (IOException e) {
       e.printStackTrace();
     }
     startListBeaconsActivity(DistanceBeaconActivity.class.getName());
   }
});

此函数用于将参数传递的文本写入"File.txt“。

代码语言:javascript
复制
public void salvarInternalStorage(String texto) throws IOException{
            // Use Activity method to create a file in the writeable directory
            FileOutputStream fos = context.openFileOutput("FileTeste.txt", context.MODE_PRIVATE);
            // Create buffered writer
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
            writer.write(String.valueOf(texto.getBytes()));
            writer.close();
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-30 20:25:35

使用SAF:

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    createFile("text/plain","Arquivo.txt");
}

private static final int WRITE_REQUEST_CODE = 43;

private void createFile(String mimeType, String fileName) {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    // Filter to only show results that can be "opened", such as
    // a file (as opposed to a list of contacts or timezones).
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Create a file with the requested MIME type.
    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_TITLE, fileName);
    startActivityForResult(intent, WRITE_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==WRITE_REQUEST_CODE&&resultCode==RESULT_OK)
        alterDocument(data.getData());
}

private void alterDocument(Uri uri) {
    try {
        ParcelFileDescriptor txt = getContentResolver().
                openFileDescriptor(uri, "w");
        FileOutputStream fileOutputStream =
                new FileOutputStream(txt.getFileDescriptor());
        fileOutputStream.write(("Tentando Gravar Esse Texto Exemplo").getBytes());
        fileOutputStream.close();
        txt.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

或者,您可以使用OPEN_DOCUMENT_TREE一次来选择一个目录,然后您就拥有了在该目录下保存任何文件的所有权限,而无需询问用户。如下所示:

代码语言:javascript
复制
Uri sdCardUri;

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

        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case 1:

            if (resultCode == Activity.RESULT_OK) {
                sdCardUri = data.getData();

                getContentResolver().takePersistableUriPermission(sdCardUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
            createFile();

            break;
    }
}

private void createFile() {
    DocumentFile sdCard=DocumentFile.fromTreeUri(this,sdCardUri);
    DocumentFile createFile=sdCard.findFile("teste.txt");
    if (createFile==null)
        createFile=sdCard.createFile("text","teste.txt");
    OutputStream outStream = null;
    try {
        outStream = getContentResolver().openOutputStream(createFile.getUri());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        String teste="Isto é um teste";
        outStream.write(teste.getBytes());
        outStream.flush();
        outStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36308428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档