2016-03-30 34 views
0

我需要在智能手机内存中写入文本文件,然后需要通过USB电缆将此.txt文件复制到您的计算机并访问他的内存(此复制过程将使手动,需要找到该文件并了解将记录哪个存储位置)。Writer文件内存中的TXT内部android

我使用下面的代码,当我运行时显示没有错误,但我不知道这个录音是否存在,以及我不知道智能手机上的目录应该是什么。

此功能按钮来调用函数salvarInternalStorage

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”通过参数传递的文本。

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(); 
} 
+0

测试中的Android 6.0摩托±3 –

+0

此代码老乡去了,上的是Android 5.1,MOTO G1的工作,但不是在Android 6.0。 –

回答

0

随着SAF:

@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一次选择一个目录之后,你拥有所有的权限,以保存目录下的任何文件,而无需询问用户。事情是这样的:

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

我试过用这段代码,这个网站写的是:/ storage/emulated/0/Download 不过,这个网站并没有创建文件夹或文件; (因为我授权所有权限 –

+0

只发生在6.0上? –

+0

编辑:使用saf的新方法 –