File generated in Android does not appear in windows Explorer

Asked

Viewed 405 times

1

I can save the file in my micro sd but connect the mobile device on computer through a USB file does not appear in Windows Explorer.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

public void ExportarArquivoExterno() {

    String lstrNomeArq;
    File arq;
    byte[] dados;

    String txtSalvar = "texto do arquivo";

    try
    {
        //pega o nome do arquivo a ser gravado
        lstrNomeArq = "nomearquivo.txt";

        arq = new File("/storage/external_SD/Exportações", lstrNomeArq);

        FileOutputStream fos;

        //transforma o texto digitado em array de bytes
        dados = txtSalvar.toString().getBytes();

        fos = new FileOutputStream(arq);

        //escreve os dados e fecha o arquivo
        fos.write(dados);
        fos.flush();
        fos.close();

    } catch (Exception e) {

        //trace("Erro : " + e.getMessage());

    }        

}
  • 3

    Some files are not available in windows, due to access restrictions of Android itself.

  • @Diegofelipe there is the possibility to change these retributions, are in the file or on android.

1 answer

0


As can be seen in this post on Android Stackexchange and in this report in google code, this is a bug that, depending on how the file is created on the device, it becomes inaccessible when accessed by the computer, via MTP.

Try to follow this solution taken from Soen, when setting file creation in your application:

MediaScannerConnection.scanFile (this, new String[] {file.toString()}, null, null);

Where file is the URI variable of your file, after created, or for verses less than 4.4 is still possible to do:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
            + Environment.getExternalStorageDirectory()))); 

You enter one of the lines of code right after the file creation code.

If you want more details, in this post from Soen and in this one there are other suggestions on how to get around this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.