gpt4 book ai didi

android - DDMS 数据文件夹的小问题

转载 作者:行者123 更新时间:2023-11-29 17:52:37 24 4
gpt4 key购买 nike

我是安卓新手。所以我试图实现一个将数据存储在设备内部存储器中的代码。它正在存储和加载数据。我看不到 eclipse 中存在的数据文件夹中的文件。当我单击数据文件夹时,它没有展开。 The data folder is clicked but could not see any files inside it

我使用了下面的代码

package com.example.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText textBox;
static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText) findViewById(R.id.txtText1);
}
public void onClickSave(View view){
String str = textBox.getText().toString();
try
{
FileOutputStream fOut =
openFileOutput("textfile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(),
"File saved successfully!",
Toast.LENGTH_SHORT).show();
//---clears the EditText---
textBox.setText("");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void onClickLoad(View view) {
try
{
FileInputStream fIn =
openFileInput("textfile.txt");
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//---set the EditText to the text that has been
// read---
textBox.setText(s);
Toast.makeText(getBaseContext(),
"File loaded successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

最佳答案

数据文件夹是设备内部存储的一部分,不应通过设备的 DDMS 访问。

不过,您可以在模拟器上运行您的应用,然后您可以在 DDMS 中浏览模拟器的数据目录。

所有新智能手机操作系统中的应用程序都采用沙盒概念。在 Android 的情况下,应用程序的沙箱目录是/data/data/your_app_package/

此目录中的文件和目录默认情况下仅供应用程序使用。如果您想在设备上使用 DDMS 查看文件,我建议您使用 getExternalStorage() 获取外部存储的句柄并将文件复制/移动到那里。在应用程序中保存文件的默认位置是 getFilesDir() which/data/data/your_app_package/files

关于android - DDMS 数据文件夹的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21951402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com