我正在尝试为学校项目集成一些代码,该项目通过蓝牙接收传感器值并将其写入逗号分隔的数据日志文件。在尝试了几个例子之后,我无法得到一个关于在Jelly Bean上运行的文件编写功能的简单示例。代码在运行android 4.0.2的个人设备上运行得非常好。我试图在motorolla xt1060上运行以下示例,虽然没有可移动的SD卡,但我的理解是这并不排除使用外部存储。我使用了带有MTPFS的linux和带有默认驱动程序的windows来在文件系统的任何地方查找文件,但它从未显示过。当我使用通过终端安装的MTPFS创建文本文件时,我可以看到文本文件,当我拍照时,我不认为MTP导致问题。
这是我编写的最简单的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上运行而不是4.2?
答案 0 :(得分:0)
/storage/emulated/0/Download/test.txt
根据您的日志,您的文本文件正在写入外部存储的emulated
部分。
在Android 4.2中,Google增加了多用户支持,并虚拟化了外部存储。当它说emulated
时,这意味着什么。
顺便说一下,您还应该检查教授是否没有为您创建有限的用户帐户。缺少数据计划不应妨碍您注册Google Play。
答案 1 :(得分:0)
今天早上重新启动手机时,文件系统上堆满了我制作的测试文件。我通过尝试编写其他新文件并尝试从桌面查看它们进行测试,但只有在重新启动手机后它们才会显示。仿真不是问题,那就是android为多个用户共享驱动器的标准新方法。我会稍微检查一下Windows上的行为,以确认它是否是手机的错误,而不仅仅是Linux驱动程序。