1

I'm working on building an UI application (.exe) to run on a Windows host, that should be able to

  1. List connected android devices.
  2. Write a file of exactly 512 bytes on internal memory (not sdcard). Path to write to be entered from UI.

The file to be written has to be exactly 512 bytes on internal memory, that's something which is vital for my application need. Here is how i am approaching:

  1. I have used Qt to code the UI and basic logic.
  2. The application uses adb devices to enumerate connected devices and display in the listView of the UI.
  3. To write a file, i first create a file on disk (my PC), fill it with 512 bytes (my pre-configured contents), and then push this file onto device using adb

    int DoWrite(Buffer* pBuf)
    {
    // pBuf contains preconfigured 512 byte pattern to be written to a file on device
    //prepare a local file       
    char*cmdBuf = new char[512];
    for (int i(0); i < 512; ++i)
        cmdBuf[i] = pBuf->operator[](i);
    ofstream myFormattedFile;
    if(myFormattedFile.is_open())
    {
         //temp file creation successful
         pBuf->WriteFormattedDataToStream(myFormattedFile);
         myFormattedFile.close();
    }
    FILE* fp = 0;
    fp = fopen("rawData.bin", "wb");
    int temp = fwrite(cmdBuf, sizeof(char), 512, fp);
    fclose(fp);
    //push this file to device at /mnt/emmc/diagnose (or whatever the myFilePath is set to)
    //ADB_PATH is set to adb executable
    //m_filePath is set to /mnt/emmc
    std::string cmd = ADB_PATH + "adb push " + "C:\\Users\\MyUser\\DiagTool\\rawData.bin " + m_filePath;
    //Execute push command
    int rc = system(cmd.c_str());
    delete [] cmdBuf;
    return 0;
    }
    

I am able to see that a file /mnt/emmc/diagnose is created. Getting adb shell, and executing ls -l on /mnt/emmc indeed shows filesize as 512!

So, here are my questions:

  • The file which I'm creating locally displays following properties enter image description here

Is it so that the file that i created on disk, is not actually of 512 bytes, but4KB?

So, if I use adb push, what will be the size of file created on device? 512 bytes or since it goes through OS, it will apply appropriate paddings and the actual file size won't be 512 bytes?

  • Is it the correct way to write to internal memory? Or is there a better approach?

  • I don't prefer to install any .apk on device, and interact with it using my application. But if nothing works, i can take that approach too!

Any help or pointer is appreciated...

Manmohan Singh
  • 439
  • 1
  • 3
  • 15

1 Answers1

0

I think you are doing the right things.

1) The file size is not supposed to change, since it's the same file. Maybe the size in the file system won't be the same anyway. You can try to write it and read it back and see if it does change.

2) If you don't run any app on the device, adb is the way to go.

3) If it works this way, leave it how it is.

DjLisT
  • 19
  • 5
  • I need the size in the filesystem to be exactly 512 bytes. How can I control that? If I'm running an app directly on device, I can issue a JNI call e.g. JNIEXPORT jint JNICALL Java_com_NativeLib_writeCommand (JNIEnv * env, jobject obj,jstring path) { char const* path1 = (*env)->GetStringUTFChars(env,path,0); int temp =0; int f = open(path1, O_CREAT | O_WRONLY |O_DIRECT); temp = write(f,cmdbuf,512); close(f); return temp; } – Manmohan Singh Nov 30 '13 at 17:16
  • Maybe you can just use a shell command through adb. See http://stackoverflow.com/questions/9421010/using-du-on-android – DjLisT Nov 30 '13 at 17:39