1

我正在尝试将来自蓝牙模块并从(HandleRead)读取的数组流直接写入内部存储。首先这可能吗?请注意,我每秒读取 100 个样本。这意味着文件将很快填满。我不熟悉存储,我的代码没有按预期执行。

public class MainActivity extends ActionBarActivity implements SensorEventListener {
    File Root = Environment.getExternalStorageDirectory();
    File Dir = new File (Root.getAbsolutePath()+"/myAppFile");
    File file = new File(Dir,"Message.txt"); 

    @Override
    protected void onCreate(Bundle savedInstanceState){
        String state;
        state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)){
            if (!Dir.exists()){
                Dir.mkdir();
            }
        }

        private void handleRead(Message msg) {
            byte[] readBuf = (byte[]) msg.obj;    
            String readMessage = new String(readBuf);
            ByteBuffer buffer = ByteBuffer.wrap(readBuf, 0, readBuf.length);
            buffer.order(ByteOrder.BIG_ENDIAN);
            buffer.clear();

            final String[] strNumbers = readMessage.split("\n");

            for (int j = 1; j <= strNumbers.length - 2; j++) {
                pressure = Integer.parseInt(readMessage2);
                MyFinalPressure = (float) (9.677 +0.831 * pressure);
                // trying to store directly to internal sotrage
                activity.save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        try {
                            FileOutputStream fileOutputStream = new FileOutputStream(activity.file);
                            fileOutputStream.write((int) MyFinalPressure);
                            fileOutputStream.close();

                            Toast.makeText(activity.getApplicationContext(),"Message saved ", Toast.LENGTH_SHORT).show();
                        } catch (FileNotFoundException e){
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}
4

1 回答 1

0

看来您没有将 FileOutputStream 设置为“追加”(您需要在构造函数中添加“真”作为第二个参数。)

这将每次从文件开始写入文件


您的“setOnClickListener”也在您的循环内。据我所知,这对你没有任何帮助。

private void setupUI() {...}我建议始终在onCreate 调用的方法中设置 UI 元素。onClick() 方法的“逻辑”在public void onClick(View v) {buttonForSavingPresssed()}哪里。buttonForSavingPressed(){...}

这将帮助您清理课程并且不会有杂散的 onClickListener 分配等。

我的猜测是,您的多个分配效率非常低,因为 clickListener 并不便宜,或者......由于时间问题,clickListener 甚至可能根本无法工作(如果您的循环长时间运行并且您按下按钮并且听众已经换了一个新的)

我已经清理了你的代码,有一些建议和一些日志语句可以帮助你弄清楚发生了什么。

// this is inside your onCreate()

...
activity.save.setOnClickListener(new View.OnClickListener() {                                              
    @Override                                                                                              
    public void onClick(View v) { buttonPressed();} 
});                                                                   

... 

// Here is where you would put your logic when the button is presssed
public void buttonPressed(){
    Toast.makeText(activity.getApplicationContext(),"Button Pressed ",   
        Toast.LENGTH_SHORT).show();         
}

// you should make 'helper' functions that consolidate separate pieces of logic like this, 
// that way you can more easily track what is happening in each method. 
// Plus it helps keep each method shorter for ease of understanding, etc.
public void writeToFile(float finalPressure){
    Log.d(LOG_TAG // where LOG_TAG is the String name of this class
          "writeToFile(float) called." );
    try{  
        // true here for 'append'                      
        FileOutputStream fileOutputStream = new 
            FileOutputStream(activity.file, true);  
        fileOutputStream.write((int) finalPressure);                                                 
        fileOutputStream.close();  
     }catch (FileNotFoundException e){                                                                  
         e.printStackTrace();                                                                           
     } catch (IOException e) {                                                                          
         e.printStackTrace();                                                                           
     }    
}

// now back to your handleRead, is this method called async wenever
// a message is read? Then wouldn't this be called a lot? I'm lost as to why 
// you had the button in here at all.
private void handleRead(Message msg) {
    Log.d(LOG_TAG // where LOG_TAG is the String name of this class
          "handleRead(Message) called." );
    byte[] readBuf = (byte[]) msg.obj;    
    String readMessage = new String(readBuf);
    ByteBuffer buffer = ByteBuffer.wrap(readBuf, 0, readBuf.length);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.clear();


    final String[] strNumbers = readMessage.split("\n");

    Log.d(LOG_TAG // where LOG_TAG is the String name of this class
          "strNumbers length: " + strNumbers.length );

    for (int j = 1; j <= strNumbers.length - 2; j++) {
         pressure = Integer.parseInt(readMessage2);
         MyFinalPressure = (float) (9.677 +0.831 * pressure);
         // trying to store directly to internal sotrage                                                                                                        
         writeToFile(MyFinalPressure);
    }
}
于 2015-12-20T17:35:35.643 回答