1

I am trying to integrate some code for a school project that takes sensor values received over bluetooth and write them to a comma separated data log file. I can not get a simple example of the file writing functionality running on Jelly Bean after trying dozens of examples. The code works perfectly fine on my personal device running android 4.0.2. I am trying to run the following example on a motorolla xt1060, and although there is no removable sd card, my understanding is that this does not preclude the use of external storage. I've used both linux with MTPFS and windows with the default drivers to look for the file anywhere on the file-system but it has never shown itself. I can see text files when I create them with the MTPFS mounted via terminal and when I take pictures so I don't think MTP is causing the problem.

Here is the most simple java example I've written that works on 4.0 and not 4.2:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        path.mkdirs();
        File file = new File(path, "test.txt");
        Log.d("FILE_TEST", file.getPath());

        try {
            Log.d("FILE_TEST", "Creating buffer");
            BufferedWriter buf = new BufferedWriter(new FileWriter(file));
            Log.d("FILE_TEST", "Writing to buffer");
            buf.write("Hello FS!\n");
            Log.d("FILE_TEST", "Flushing buffer");
            buf.flush();
            Log.d("FILE_TEST", "Closing file");
            buf.close();
        } catch (IOException e) {
            Log.d("FILE_TEST","Caught Exception:" + e.toString());
        }
    }
}

The manifest xml contains the following:

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

And nothing exciting I can see happens in the log:

02-18 00:26:36.075: I/InputReader(859): Reconfiguring input devices.  changes=0x00000004
02-18 00:26:36.075: I/InputReader(859): Device reconfigured: id=6, name='synaptics_dsx_i2c', size 720x1280, orientation 3, mode 1, display id 0
02-18 00:26:36.533: I/SurfaceFlinger(290): triggers:    (post: 0:16)    (compose: 0:2)  (frame: 0:12)   (gap: 411:6519) (rate: 247) (66116 frames)
02-18 00:26:36.561: I/SurfaceFlinger(290): triggers:    (jitter: 0:0)   (missed: 0) (105353 vsyncs)
02-18 00:26:37.045: I/ActivityManager(859): Config changes=480 {1.0 311mcc480mnc en_US ldltr sw360dp w360dp h567dp 320dpi nrml port finger -keyb/v/h -nav/h s.23?spn}
02-18 00:26:37.051: I/InputReader(859): Reconfiguring input devices.  changes=0x00000004
02-18 00:26:37.051: I/InputReader(859): Device reconfigured: id=6, name='synaptics_dsx_i2c', size 720x1280, orientation 0, mode 1, display id 0
02-18 00:26:37.101: W/ContextImpl(1155): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1115 android.content.ContextWrapper.sendBroadcast:338 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.PauseResumeTrigger.handleFrameworkEvent:53 com.motorola.motocare.internal.frameworkevents.FwEventMonitor$FrameworkListener.processFrameworkEvent:114 
02-18 00:26:37.124: I/SurfaceFlinger(290): triggers:    (post: 0:16)    (compose: 0:2)  (frame: 0:12)   (gap: 411:6520) (rate: 247) (66124 frames)
02-18 00:26:37.127: D/FILE_TEST(19084): /storage/emulated/0/Download/test.txt
02-18 00:26:37.127: D/FILE_TEST(19084): Creating buffer
02-18 00:26:37.127: D/FILE_TEST(19084): Writing to buffer
02-18 00:26:37.127: D/FILE_TEST(19084): Flushing buffer
02-18 00:26:37.127: D/FILE_TEST(19084): Closing file
02-18 00:26:37.186: I/SBar.NetworkController(1008): onReceive: Intent.ACTION_CONFIGURATION_CHANGED Received
02-18 00:26:37.186: I/SBar.NetworkController(1008): updateTelephonySignalStrength:  No service
02-18 00:26:37.290: I/SurfaceFlinger(290): triggers:    (post: 0:16)    (compose: 0:2)  (frame: 0:12)   (gap: 411:6522) (rate: 247) (66132 frames)
02-18 00:26:37.718: I/SurfaceFlinger(290): triggers:    (post: 0:16)    (compose: 0:2)  (frame: 0:12)   (gap: 411:6522) (rate: 247) (66159 frames)
02-18 00:26:37.743: I/InputReader(859): Reconfiguring input devices.  changes=0x00000004
02-18 00:26:37.743: I/InputReader(859): Device reconfigured: id=6, name='synaptics_dsx_i2c', size 720x1280, orientation 1, mode 1, display id 0
02-18 00:26:37.744: I/ActivityManager(859): Config changes=480 {1.0 311mcc480mnc en_US ldltr sw360dp w598dp h335dp 320dpi nrml land finger -keyb/v/h -nav/h s.24?spn}
02-18 00:26:37.776: W/ContextImpl(1155): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1115 android.content.ContextWrapper.sendBroadcast:338 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.PauseResumeTrigger.handleFrameworkEvent:53 com.motorola.motocare.internal.frameworkevents.FwEventMonitor$FrameworkListener.processFrameworkEvent:114 
02-18 00:26:37.817: I/SurfaceFlinger(290): triggers:    (post: 0:16)    (compose: 0:2)  (frame: 0:12)   (gap: 411:6523) (rate: 247) (66164 frames)
02-18 00:26:37.829: I/SurfaceFlinger(290): triggers:    (jitter: 0:0)   (missed: 0) (105404 vsyncs)

Does anyone have an idea why this would work on 4.0 and not 4.2?

user1056153
  • 51
  • 1
  • 4
  • The code seems to work fine on 4.3. Are you able to find the file with a file explorer on the device itself? – ThaMe90 Feb 18 '14 at 07:41
  • The professor who commissioned the project didn't get a data plan and I couldn't find a way to register for Google Play without a data connection even though wifi is enabled. I'm fairly sure there is nothing there though, I put a text file in downloads manually and I could see it from MTPFS and gmtp in linux and in windows explorer. – user1056153 Feb 18 '14 at 08:44
  • 1
    This sounds a lot like the MTP visibility problem, such as described (rather poorly) here: http://stackoverflow.com/questions/18292694/files-created-on-external-storage-do-not-show-up-when-device-is-mounted-on-windo – Chris Stratton Feb 18 '14 at 21:19
  • Thanks, that seems to be the issue, it can be solved programmaticaly with the following: sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); – user1056153 Feb 19 '14 at 00:58

2 Answers2

0
/storage/emulated/0/Download/test.txt

According to your log, your text file is being written to the emulated part of the external storage.

In Android 4.2, Google added multi-user support, and it virtualized the external storage. That's what it means when it says emulated.

By the way, you should also check if the Professor didn't create a limited user account just for you. The lack of a data plan shouldn't prevent you from registering with Google Play.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
0

When I restarted the phone this morning, the file system was littered with test files I had made. I tested by trying to write other new files and trying to view them from a desktop but they would only become visible after restart of the phone. Emulation isn't the problem, that is then standard new way of android sharing a drive for multiple users. I'll double check the behvior on Windows in a bit to confirm if its a bug with the phone and not just the Linux drivers.

user1056153
  • 51
  • 1
  • 4