2014-02-18 71 views
1

我正在尝试整合一些学校项目的代码,它需要通过蓝牙接收传感器值并将它们写入逗号分隔的数据日志文件。尝试了几十个示例后,我无法获得在Jelly Bean上运行的文件写入功能的简单示例。该代码在我运行android 4.0.2的个人设备上工作得很好。我试图在motorolla xt1060上运行以下示例,虽然没有可移动的SD卡,但我的理解是,这并不排除使用外部存储器。我已经使用Linux和MTPFS,并使用默认驱动程序的Windows在文件系统上的任何位置查找文件,但它从未显示过它自己。当我使用通过终端安装的MTPFS创建文本文件时,我可以看到文本文件,当我拍照时,我不认为MTP会导致问题。写入日志文件不适用于Android 4.2 Jelly Bean

这是最简单的Java例子我写的关于4.0工作,而不是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()); 
     } 
    } 
} 

清单XML包含以下内容:

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

而且平平淡淡的,我可以看到发生在日志中:

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) 

有没有人有一个想法,为什么这将工作在4.0和不t 4.2?

+0

该代码似乎在4.3上正常工作。您是否能够在设备上使用文件资源管理器查找文件? – ThaMe90

+0

委托该项目的教授没有获得数据计划,即使已启用wifi,我也无法找到注册Google Play而无需数据连接的方法。我相当确定这里没有任何东西,我手动下载了一个文本文件,我可以从MTPFS和gmtp在Linux和Windows资源管理器中看到它。 – user1056153

+1

这听起来很像MTP可见性问题,比如这里描述的(相当糟糕):http://stackoverflow.com/questions/18292694/files-created-on-external-storage-do-not-show-up-当设备被安装在窗口 –

回答

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

根据你的日志,文本文件被写入到外部存储的emulated一部分。

在Android 4.2中,Google增加了多用户支持,并且虚拟化了外部存储。这就是它说的emulated

顺便说一下,您还应该检查教授是否没有为您创建有限的用户帐户。缺乏数据计划不应阻止您注册Google Play。

0

今天早上当我重新启动手机时,文件系统里散布着我所制作的测试文件。我通过尝试写入其他新文件并尝试从桌面查看它们进行测试,但只有在重新启动手机后才能看到它们。仿真不是问题,那就是标准的新方式android共享多用户驱动器。我会仔细检查Windows上的behvior,以确认它是否存在手机的错误,而不仅仅是Linux驱动程序。