尽管提供了权限,但 File.canWrite() 在某些设备上返回 false。
在我的应用程序中,我需要将数据写入外部 SD 卡。然而,这在某些设备上有效,但在其他一些设备上失败。例如作品:Limebox Internal Flash, SDCard, android OS: 2.3.3 Samsung Sdcard, extSdcard, android OS: 4.0.3
失败:Leoxys sdcard,extsd android OS:4.0.3
如何动态找出 Android 兼容设备,即允许外部 SD 卡写入操作的设备。我的文件.canWrite
详细信息:getExternalSD() 是一个用户定义的函数,它将返回外部 sdcard 路径
在我的情况下,外部 SD 卡路径是“/mnt/extSdcard”(或)“/mnt/extsd”我通过读取文件“/system/etc/vold.fstab”来找到外部 SD 卡路径
这是一些代码片段:它在外部 sdcard 中创建一个文件夹。在某些设备上,myfile.canWrite() 返回 False,我无法创建文件夹。
String sdcardpath = getExternalSD();
System.out.println("the sdcard path is:"+sdcardpath.trim().toString());
tv = (TextView) findViewById(R.id.textView1);
tv.append("the sd card path is:"+sdcardpath);
File myfile = new File(sdcardpath);
tv.append("\n the extsd is readable or not:"+myfile.canRead());
tv.append("\n \n the extsd is writable or not:"+myfile.canWrite());
if(myfile.canWrite()){
File testfile = new File(sdcardpath, "test");
if(testfile.exists()){
}else{
boolean mkdir = testtlie.mkdirs();
if(mkdir == true){
tv.append("\n\n the path is:"+sdcardpath.trim().toString()+"/myfile");
}else{
tv.append("\n File does not have permissions");
}
}
}else{
System.out.println("the external sd card does not have write permissions");
tv.append("\n\n the external sd card does not have write permissions");
}
xml 文件片段。
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
我想得到关于
a的建议。是否有任何 API 可以让开发人员知道设备是否真正兼容 Android?
湾。为什么不同硬件设备上的行为存在差异。这与固件有关吗?
C。有没有其他方法可以在这种硬件上实现写入?
我已经尝试使用终端模拟器修改 /system/etc/premissions/ 下文件“platform.xml”的权限但我无法以写入模式打开文件
默认情况下,android 提供 Environment.getEnternalStorageDirectory()。但是该库返回“/mnt/sdcard”,这将是某些设备的内部存储器。
您的意见将不胜感激!