android-studio - 访问内部存储 Android Wear

标签 android-studio wear-os android-sensors internal-storage

我正在尝试将传感器的读数写入文件。我有这个代码.. 我需要查看我正在编写的数据,但我不知道如何访问它。我下载了 Android Wear 的文件管理器,但它不允许我进入存储文件夹。下面是我的代码,希望你能帮助它

public class Logging extends Activity implements SensorEventListener {

DecimalFormat df =new DecimalFormat("##.##");
public static final String file=("Project.txt");

Boolean flag=true;
FileOutputStream outputStream;


    TextView displayReading1;
    TextView displayReading2;
    TextView displayReading3;
    TextView displayReading4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logging);
        Button start=(Button)findViewById(R.id.StartButton);
        Button stop=(Button)findViewById(R.id.StopButton);
        //BUTTON START
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             flag=true;

            }
        }); {


        }
        //BUTTON STOP
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               flag=false;


            }
        });{

        }
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        Sensor rotSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        Sensor gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        Sensor gyroSensor = sensorManager.getDefaultSensor((Sensor.TYPE_GYROSCOPE));
        sensorManager.registerListener(this, accSensor, 100000);
        sensorManager.registerListener(this, rotSensor, 100000);
        sensorManager.registerListener(this, gravitySensor, 100000);
        sensorManager.registerListener(this, gyroSensor, 100000);

        displayReading1 = (TextView) findViewById(R.id.acc_sensor);
        displayReading2 = (TextView) findViewById(R.id.rotational_sensor);
        displayReading3 = (TextView) findViewById(R.id.gravity_sensor);
        displayReading4 = (TextView) findViewById(R.id.gyro_sensor);

        displayReading1.setTextSize(14f);
        displayReading2.setTextSize(14f);
        displayReading3.setTextSize(14f);
        displayReading4.setTextSize(14f);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_logging, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                displayReading1.setText("Accelerometer (m/s2): " +"\nX"+" " +df.format(event.values[0]) + "\nY"+" " + df.format(event.values[1]) + "\nZ"+" " + df.format(event.values[2]));
                float X11=event.values[0];
                float X12=event.values[1];
                float X13=event.values[2];
        if( flag) {
            try {
                outputStream = openFileOutput(file, Context.MODE_PRIVATE);
                outputStream.write(("Accelerometer (m/s2): " + "\nX" + " " + df.format(X11) + "\nY" + " " + df.format(X12) + "\nZ" + " " + df.format(X13)
                ).getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException f) {
                System.out.println("Exception");

            }
        }
        if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
                displayReading2.setText("Rotational Vector: " + "\nX"+" " + df.format(event.values[0]) + "\nY"+" " + df.format(event.values[1]) + "\nZ"+" " + df.format(event.values[2]));
                float X21=event.values[0];
                float X22=event.values[1];
                float X23=event.values[2];
        if( flag) {
        try {
            outputStream=openFileOutput(file, Context.MODE_PRIVATE);
            outputStream.write(("Rotational Vector : " +"\nX"+" " +df.format(X21) + "\nY"+" " + df.format(X22) + "\nZ"+" " + df.format(X23)).getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException f) {
            System.out.println("Exception");

        }}
            if (event.sensor.getType() == Sensor.TYPE_GRAVITY)
                displayReading3.setText("Gravity (m/s2): " + "\nX"+" " + df.format(event.values[0]) + "\nY"+" " + df.format(event.values[1]) + "\nZ"+" " + df.format(event.values[2]));

            if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE)
               displayReading4.setText("Gyroscope (rad/s): " + "\nX"+" " + df.format(event.values[0]) + "\nY"+" " + df.format(event.values[1]) + "\nZ"+" " + df.format(event.values[2]));
        float X31=event.values[0];
        float X32=event.values[1];
        float X33=event.values[2];

        if( flag) {
            try {
                outputStream = openFileOutput(file, Context.MODE_PRIVATE);
                outputStream.write(("Gyroscope (rad/s): " + "\nX" + " " + df.format(X31) + "\nY" + " " + df.format(X32) + "\nZ" + " " + df.format(X33)
                ).getBytes());

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException f) {
                System.out.println("Exception");

            }

        }
        if (!flag){
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        }



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }



}

最佳答案

这取决于您尝试访问的文件存储。我已将其用于 Android 和 Android Wear。这将访问应用程序的文档目录,该目录只能从应用程序访问,而不能由其他应用程序访问。当我将其用于内部存储时,也会共享该代码。

这只是简单地一一显示此目录中的所有文件:

public void listFiles(View view) {
    PackageManager m = getPackageManager();
    String s = getPackageName();
    try {
        PackageInfo p = m.getPackageInfo(s, 0);
        s = p.applicationInfo.dataDir;
    } catch (PackageManager.NameNotFoundException e) {
        Log.w("yourtag", "Error Package name not found ", e);
    }

    Context context = getApplicationContext();

    File folder = new File(context.getFilesDir().toString());
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            Toast.makeText(MainActivity.this, "File " + listOfFiles[i].getName(),
                    Toast.LENGTH_SHORT).show();
            //System.out.println("File " + listOfFiles[i].getName());
        } else if (listOfFiles[i].isDirectory()) {
            Toast.makeText(MainActivity.this, "File " + listOfFiles[i].getName(),
                    Toast.LENGTH_SHORT).show();
            //System.out.println("Directory " + listOfFiles[i].getName());
        }
    }
}

这也是将文件保存到 watch 的外部存储中,可以通过其他应用程序访问:
   private final String fileName = "aq.pcm";
    private String TAGFILE = "ExternalFileWriteReadActivity";

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        // Enables Ambient mode.
        mAmbientController = AmbientMode.attachAmbientSupport(this);

        //Copy files
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {

            File outFile = new File(
                    getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                    fileName);

            if (!outFile.exists())
                copyImageToMemory(outFile);
        }
    }

    // Copy file methods
    private void copyImageToMemory(File outFile) {
        try {

            BufferedOutputStream os = new BufferedOutputStream(
                    new FileOutputStream(outFile));

            BufferedInputStream is = new BufferedInputStream(getResources()
                    .openRawResource(R.raw.sound));

            copy(is, os);

        } catch (FileNotFoundException e) {
            Log.e(TAGFILE, "FileNotFoundException");
        }
    }

    private void copy(InputStream is, OutputStream os) {
        final byte[] buf = new byte[1024];
        int numBytes;
        try {
            while (-1 != (numBytes = is.read(buf))) {
                os.write(buf, 0, numBytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                Log.e(TAGFILE, "IOException");

            }
        }
    }

关于android-studio - 访问内部存储 Android Wear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594956/

相关文章:

Android Wear - 如何将音频输入从手机传输到可穿戴设备?

android - 我们可以为 Android Wear 项目上的通知做一个自定义布局吗

android - 使用 UDP 和 Python 服务器从 Android 到 PC 的音频流中的噪声

使用传感器数据的 Android 智能电源管理

java - findViewById 在静态函数中不起作用

android - 如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest

wear-os - 使用 “Start …” 语音命令启动 Android Wear 应用

android - 如何在 Android Studio 的 firebase 文件存储中上传文件?

Android Studio : AVD options give the error "The skin directory does not point to a valid skin", 但仅在编辑虚拟设备时

Android 加速度计 : SensorManager. DATA_X 已弃用 - 现在怎么办?