访问内部存储Android Wear

时间:2015-04-12 21:39:25

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

我正在尝试在文件上写下传感器的读数。我有这个代码..我需要看到我正在写的数据,但我不知道如何访问它。我下载了文件管理器for android wear但它不允许我进入Storage文件夹。以下是我的代码,希望你能帮助它

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) {

    }



}

1 个答案:

答案 0 :(得分:1)

这取决于您尝试访问的文件存储。我已将它用于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());
        }
    }
}

这也是将文件保存到手表的外部存储器中,可以通过其他应用程序访问:

   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");

            }
        }
    }