2

我試圖在文件上寫入傳感器的讀數。我有這個代碼..我需要看到我正在寫的數據,但我不知道如何訪問它。我爲Android Wear下載了文件管理器,但它不允許我進入存儲文件夾。下面是我的代碼,希望你能幫到它訪問內部存儲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) { 

    } 



} 

回答

0

這取決於你試圖訪問什麼文件存儲。我已經將這個用於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"); 

      } 
     } 
    }