Android 我能';t在我的假SD卡中列出文件
我正在尝试将一些数据插入并检索到ExternalStorage内部的特定路径中。然后,我尝试检索所有SD卡路径,以了解我的路径是否已创建。但是我在列表中看不到我的路径。我已经意识到我的路径是在一个不同的位置创建的(它必须在我创建的mysd.img中),为了能够看到我的路径,这个代码应该是什么样子的 这在我的路径字符串下面:Android 我能';t在我的假SD卡中列出文件,android,Android,我正在尝试将一些数据插入并检索到ExternalStorage内部的特定路径中。然后,我尝试检索所有SD卡路径,以了解我的路径是否已创建。但是我在列表中看不到我的路径。我已经意识到我的路径是在一个不同的位置创建的(它必须在我创建的mysd.img中),为了能够看到我的路径,这个代码应该是什么样子的 这在我的路径字符串下面: private String filepath = "MyFileStorage"; 我正在为此使用此代码: 此代码用于列表 public class MainActivi
private String filepath = "MyFileStorage";
我正在为此使用此代码:
此代码用于列表
public class MainActivity extends ListActivity {
List<String>fileList=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root=
new File(Environment.getExternalStorageDirectory().getAbsolutePath());
listDir(root);
ArrayAdapter<String>adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,fileList);
this.setListAdapter(adapter);
}
private void listDir(File f) {
File[]files=f.listFiles();
for (File file : files) {
fileList.add(file.getPath());
}
}
}
这是安卓模拟器使用的“假SD卡”图像吗?您确定它已连接到仿真器吗?您可能想使用ADB shell或安装一个文件管理器应用程序(但请注意,在ADB中查看与在应用程序中使用时,外部存储的路径略有不同)Hi Chris,我正在使用emulator。SD卡已连接。我确信。我可以从SD卡读取和写入数据。问题是,无论何时尝试提供上述特定路径。我无法列出数据。正如您所说,外部存储的路径略有不同,我需要更改此行。文件根=新文件(Environment.getExternalStorageDirectory().getAbsolutePath())。。。但是我不知道它看起来应该是什么样子你试过在上面运行F3探测器了吗?这是一个寻找假SD卡的程序
public class MainActivity extends Activity implements OnClickListener{
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
File myInternalFile;
File myExternalFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , filename);
Button saveToInternalStorage =
(Button) findViewById(R.id.saveInternalStorage);
saveToInternalStorage.setOnClickListener(this);
Button readFromInternalStorage =
(Button) findViewById(R.id.getInternalStorage);
readFromInternalStorage.setOnClickListener(this);
Button saveToExternalStorage =
(Button) findViewById(R.id.saveExternalStorage);
saveToExternalStorage.setOnClickListener(this);
-
Button readFromExternalStorage =
(Button) findViewById(R.id.getExternalStorage);
readFromExternalStorage.setOnClickListener(this);
//check if external storage is available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveToExternalStorage.setEnabled(false);
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
public void onClick(View v) {
EditText myInputText = (EditText) findViewById(R.id.myInputText);
TextView responseText = (TextView) findViewById(R.id.responseText);
String myData = "";
switch (v.getId()) {
case R.id.saveInternalStorage:
try {
FileOutputStream fos = new FileOutputStream(myInternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText("");
responseText
.setText("MySampleFile.txt saved to Internal Storage...");
break;
case R.id.getInternalStorage:
try {
FileInputStream fis = new FileInputStream(myInternalFile);
// DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(fis));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
responseText
.setText("MySampleFile.txt data retrieved from Internal Storage...");
break;
case R.id.saveExternalStorage:
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText("");
responseText
.setText("MySampleFile.txt saved to External Storage...");
break;
case R.id.getExternalStorage:
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
responseText
.setText("MySampleFile.txt data retrieved from Internal Storage...");
break;
}
}