0

My problem is about making a copy of a file on an Android mobile.

Whichever Java method I use below, the file copy is created on the Android mobile.

I can see it, on the mobile, via the menu Storage & USB -> Explore.

But I cannot see it from a PC to which the mobile is connected via USB.

Note that I did not forget to activate the option USB for file transfer -> Use USB for File transfers.

To be more precise, on the (Windows) PC, and after I activate that option on the mobile, a file explorer opens up (on the PC) and I cannot see the file just created (on the mobile).

I cannot see it either via programming using the C++ API PortableDeviceApi.

Now, if I reboot the Android mobile and activate the option mentioned above, then I can see the file in a file explorer on the PC.

Do you know that problem? Can you help me solve it?

The Android version is 6.0.1.

Java methods used to make a copy of a file:

public static void makeACopyOfAFile(String s_srcFilePath, String s_destFilePath)
throws IOException
{
File srcFile = null;
File destFile = null;

        srcFile  = new File(s_srcFilePath);
        destFile = new File(s_destFilePath);

        FileUtils.copyFile(srcFile, destFile);
}

public static void makeACopyOfAFile(String s_srcFilePath, String s_destFilePath)
throws IOException
{
File         srcFile = null;
File         destFile = null;
InputStream  inputStream = null;
OutputStream outputStream = null;
int          n_len = -1;
byte[] buf = new byte[1024];

        srcFile  = new File(s_srcFilePath);
        destFile = new File(s_destFilePath);

        inputStream = new FileInputStream(srcFile);
        outputStream = new FileOutputStream(destFile);

        while((n_len = inputStream.read(buf)) > 0)
        {
                outputStream.write(buf, 0, n_len);
        }

        outputStream.close();
        inputStream.close();
}

====================================================

EDIT_1

From that thread: Android How to use MediaScannerConnection scanFile

I added the code:

MediaScannerConnection.scanFile(currentActivity,
          new String[] { s_destFilePath }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
      public void onScanCompleted(String path, Uri uri) {
          Log.i("ExternalStorage", "Scanned " + path + ":");
          Log.i("ExternalStorage", "-> uri=" + uri);
      }
 });

And it solved my problem.

Léa Massiot
  • 1,928
  • 6
  • 25
  • 43

0 Answers0