0

I'm developing an android application that has to communicate to a pc software through a json file. So in my android app, the user can export its data that is stored in the Download folder. The objective is that in my VB.NET application, I'll retrieve this file when the device is USB connected to the PC, and treat the data.

I think my export works well, the file is stored in the download folder and is readable. However, when I connect my device to the PC (windows 10 btw), and select the transfer file on my android device, the file isn't showing in Download. I tried to put other files in the device internal storage and I can see them on the computer file explorer so I really don't get it.

I searched for my problem and I found that it could be a cache problem and I did what is said in this post but I have the same problem.

Here is the code I wrote to save the file in the device:

private void writeExportFile() {
    try {
        if (!(ContextCompat.checkSelfPermission(mainAct, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            File file = new File(Environment.getExternalStorageDirectory() + "/Download/data_export.json");

            Writer output = new BufferedWriter(new FileWriter(file));
            output.write(reportArray.toString());
            output.close();

            Toast.makeText(mainAct, "Export to " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Does anyone know why I can't see my written file and what I should do?

Also, I'd be glad to hear your opinion on the storage of files in an android device. I'm new to android development and I don't know the good practices. I don't know what is the most efficient way to store a file on an android device when I have to get it from a pc. I choose download because the Document isn't on all the android devices. And I'd like to delete it from the computer when I have finished to read it.

Thanks for reading.

EDIT: I tried to add a media store scan to scan my file as suggested blackapps. When I do, my file isn't saved in Download. I followed this post to use a media scanner.

public class SingleMediaScanner implements MediaScannerConnectionClient {

    private MediaScannerConnection mMs;
    private File mFile;

    public SingleMediaScanner(Context context, File f) {
        mFile = f;
        mMs = new MediaScannerConnection(context, this);
        mMs.connect();
    }

    @Override
    public void onMediaScannerConnected() {
        mMs.scanFile(mFile.getAbsolutePath(), null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        mMs.disconnect();
    }

}

And in my method writeExportFile(), I added the instruction new SingleMediaScanner(mainAct, file); after the output.close().

Morgane
  • 153
  • 4
  • 13
  • You should let the media store scan your file first. Or reboot your device will do. – blackapps Nov 25 '19 at 10:00
  • Is the file saved in the device? Can you see it with a file manager app on your device? – blackapps Nov 25 '19 at 10:02
  • Put a Toast too in that catch block. – blackapps Nov 25 '19 at 10:04
  • I'm doing the media store scan @blackapps I'll let you know if it changes anything. The file is saved in the device, in Download, I can see it and open it. The toast of the try block always display so there isn't any error but the file isn't showing on my computer. – Morgane Nov 25 '19 at 10:08
  • When I add the media store scan, my file is deleted or its saving is canceled. What does it mean? – Morgane Nov 25 '19 at 10:12
  • A scan will not delete the file. And as you do the scan after the download the saving is already done and cannot be canceled any more. Please put the scan code in your post at the right place. – blackapps Nov 25 '19 at 10:21

0 Answers0