我在一个棘手的问题上需要帮助。我编写了一个应用程序,可以使用SD卡上提供的文件 - 操作此文件 - 并将其写回SD卡。到现在为止还挺好。通过SD仿真,Eclipse环境中的一切都运行良好。一旦我在Sony Xperia上安装应用程序 - 该应用程序在SD卡上找不到该文件 - 我也无法找到我在SD卡上存储/创建的文件/目录。 我正在发行我为索尼写的测试应用程序,并请您就此问题寻求帮助!
以下是该应用的代码段 - 尝试创建目录:
public class IOTestActivity extends ActionBarActivity {
private boolean mExternalStorageAvailable;
private boolean mExternalStorageWriteable;
TextView debugInfo;
TextView availInfo;
TextView writeInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iotest);
debugInfo = (TextView)findViewById(R.id.iotestLbLDebugInfo);
checkSDCard();
fillView();
readSDCard();
}
private void readSDCard() {
debugInfo.append("...start reading SD dirs\n\n");
if ((mExternalStorageAvailable==true) &&
(mExternalStorageWriteable == true)) {
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
debugInfo.append("SD-Root='" + root + "'\n");
File newDir = new File (root, "TEST");
boolean result = newDir.mkdir();
debugInfo.append("...creating new dir '" + newDir + "' = " + result);
}
}
private void fillView() {
debugInfo.setText("...entering IO Test\n");
debugInfo.append("...reading SD-card availability\n");
availInfo = (TextView)findViewById(R.id.iotestLblInfo1);
availInfo.setText("" + mExternalStorageAvailable);
debugInfo.append("...reading SD-card writing rights\n");
writeInfo = (TextView)findViewById(R.id.iotestLblInfo2);
writeInfo.setText("" + mExternalStorageWriteable);
}
private void checkSDCard() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
}
当然我已添加到我的Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
这是app的测试输出:
顺便说一句:如果我使用的话,问题独立于:
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
或
File root = Environment.getExternalStorageDirectory();
当我通过文件浏览器通过USB连接的手机访问SD卡时,我总是收到相同的SD卡内容:
感谢您的帮助!