Android motherboard with USB pendrive has a delayed write and create 0 byte length files

120 views Asked by At

I have an Android special motherboard ( A100 ) This motherboard has some serial ports and USB ports. The USB ports can works like a Host or client.

So I plug an USB pendrive and this is working fine. I can write to and read from this pendrive.

My problem: I created an app and this app reach the pendrive. When I write a file to the pendrive then the system indicate the write is succesfull. But if I plug out the pendrive and plug to my PC then I recognise the files but all 0 length. When I stay the pendrive more time in then android and exit my app, then the files wil be correct (and size correct also)

I think there is a caching mechanism. What can I do? How can I sense the real file writeing? I think the COPYFILE function is wrong.

public static final String USBDISK = "/mnt/udisk1/disk-1/";
public void call_upload(View v){
    File directory = getDir("data", Context.MODE_PRIVATE);
    File sdcard = new File(consts.USBDISK);
    if ( !copyToExternal(directory, sdcard)) {
            Log.d(consts.logID, "No USB");
    }
}

public boolean copyToExternal(File directory, File sdcard){
    boolean res = true;
    try {
        File newFolder = new File(sdcard, "newfolder");

        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        try {
            Log.d("Files", "FileName:" + directory.getName());
            File f = directory;
            File file[] = f.listFiles();
            Log.d("Files", "Size: "+ file.length);
            String s;
            for (int i=0; i < file.length; i++)
            {
                s = file[i].getName();
                if (s.substring(0,4).compareTo(consts.xlsNapi.substring(1))==0){
                    consts.copyFile(new File(directory, s),new File(newFolder, s));
                    Log.d("Files", "FileName:" + s);
                } else {
                    Log.d("Files", "Not copied:" + s + "("+s.substring(0,4)+","+consts.xlsNapi.substring(1)+")");
                }
            }

            //consts.copyFile(new File(newFolder, consts.xlsSetup), new File(directory, consts.xlsSetup));
            consts.longHint(this,"Files copied!");
        } catch (Exception ex) {
            res = false;
            System.out.println("ex: " + ex); //ex: java.io.FileNotFoundException: /storage/sdcard0/cashregister_v1/setup.xls: open failed: ENOENT (No such file or directory)
        }
    } catch (Exception e) {
        System.out.println("e: " + e);
        res = false;
    }
    return res;
}
public static boolean copyFile(File src,File dst)throws IOException{
    if(src.getAbsolutePath().toString().equals(dst.getAbsolutePath().toString())){

        return true;

    }else{
        InputStream is=new FileInputStream(src);
        OutputStream os=new FileOutputStream(dst);
        byte[] buff=new byte[1024];
        int len;
        while((len=is.read(buff))>0){
            os.write(buff,0,len);
        }
        is.close();
        os.flush();
        os.close();
    }
    return true;
}
0

There are 0 answers