标签:
公司做的智能硬件,有些地方需要和硬件芯片交互,不多说,直接上代码:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.os.SystemClock; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private TextView mWriteTime; private Button mBtnTest; private static final String TAG = "Ilejia::TestSDCard"; private TestAsyncTask mTestTask = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWriteTime = (TextView)findViewById(R.id.write_time); mBtnTest = (Button) findViewById(R.id.btn_test); mBtnTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mTestTask != null && mTestTask.getStatus() != AsyncTask.Status.FINISHED) { return; } mTestTask = new TestAsyncTask(); mTestTask.execute((Void[])null); } }); } private class TestAsyncTask extends AsyncTask<Void, Void, Long> { @Override protected void onPreExecute() { mBtnTest.setEnabled(false); mWriteTime.setText("Testing..."); } @Override protected Long doInBackground(Void... params) { return runTest(); } protected void onPostExecute(Long result) { mBtnTest.setEnabled(true); if (result > 0) { mWriteTime.setText("写100M数据耗时" + result + "ms"); } }; } long runTest() { File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Ilejia"); File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "test.dat"); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.e(TAG , "failed to create directory"); return -1; } } // if (mediaFile.exists()) { // mediaFile.delete(); // } Log.i(TAG , "preparing data"); StringBuilder s = new StringBuilder(); s.append("this is a test string writing to file."); for(int i = 0; i < 1000; i++){ s.append("0123456789"); } Log.i(TAG , "write data"); long startTime = SystemClock.uptimeMillis(); FileOutputStream stream = null; try { stream = new FileOutputStream(mediaFile); byte[] buf = s.toString().getBytes(); for(int i = 0; i < 10000; ++i){ stream.write(buf); } stream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (Exception e) { e.printStackTrace(); } } } Log.i(TAG , "write data successful"); return SystemClock.uptimeMillis() - startTime; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, 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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
标签:
原文地址:http://www.cnblogs.com/rfeng1016/p/4600666.html