关于DDMS数据文件夹的小问题[英] Small issue over DDMS data folder

本文是小编为大家收集整理的关于关于DDMS数据文件夹的小问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是新的Android.因此,我试图实现将数据存储在设备的内部存储中的代码.它正在存储和加载数据.我无法在Eclipse中看到存在的数据文件夹中的文件.当我单击数据文件夹时,它不会扩展. 数据文件夹被单击,但无法看到它内部的任何文件

我使用了以下代码

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的Android App的Sandbox目录是/数据/数据/your_package/

此目录中的文件和目录默认情况下仅可供应用程序使用.如果要在设备上使用DDMS查看文件,则建议您使用getexternalstorage()在外部存储器上句柄并在那里复制/移动文件.保存文件中的文件的默认位置是getfilesdir()哪个/data/data/your_package/files

其他推荐答案

不推荐使用常量mode_world_readable.

如果要写可以在外部看到的文件,则使用 Environment.getexternalfilesdir

注意事项并不意味着文件将在外部存储上.

本文地址:https://www.itbaoku.cn/post/102538.html

问题描述

I am new to android . So i was trying to implement a code which stores the data in the internal storage of the device. It is storing and loading the data. I cannot see the file in the data folder which is present in the eclipse. When i click the data folder , it is not expanding. The data folder is clicked but could not see any files inside it

I have used the below code

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();
        }
    }
}

推荐答案

The data folder is part of the internal storage of the device and should not be accessible over DDMS for a device.

You can however run your app on the emulator and then you can browse the emulator's data directory in DDMS.

The apps in all new smartphone OS's work on the concept of sandboxing. In case of Android the sandbox directory for an app is /data/data/your_app_package/

The files and directories in this directory are by default only available to the app. If you want to view the files using DDMS on a device, I suggest you get a handle on the External Storage using getExternalStorage() and copy/move your files there. The default location to save your files in the app is getFilesDir() which /data/data/your_app_package/files

其他推荐答案

The constant MODE_WORLD_READABLE is deprecated.

If you want to write a file that can be seen externally, then use Environment.getExternalFilesDir

Note it doesn't mean the file will be on external storage.