public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get the texts fields of the layout and setup to invisible mSensorsTot = (TextView) findViewById(R.id.sensoritot); mSensorAvailables = (TextView) findViewById(R.id.sensoridisponibili); // Get the SensorManager mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE); // List of Sensors Available List<Sensor> msensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL ); // Print how may Sensors are there mSensorsTot.setText(msensorList.size()+" "+this.getString(R.string.sensors)+"!"); // Print each Sensor available using sSensList as the String to be printed String sSensList = new String(""); Sensor tmp; int x,i; for (i=0;i<msensorList.size();i++){ tmp = msensorList.get(i); sSensList = " "+sSensList+tmp.getName(); // Add the sensor name to the string of sensors available } // if there are sensors available show the list if (i>0){ sSensList = getString(R.string.sensors)+":"+sSensList; mSensorAvailables.setText(sSensList); } }
这里呢要注意使用Sensor.TYPE_ALL 而不是使用废棋的SensorManager.SENSOR_ALL他对部分机器不使用
把我好郁闷
int count = mListAdapter.getGroupCount();
for (int i = 0; i <count ; i++)
mListView.collapseGroup(i);
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
/**
* This Activty shows how to save an image (Bitmap) to the filesystem, with FileOutputStream object
* @author FaYnaSoft Labs
*
*/
public class Main extends Activity {
private Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
FileOutputStream fos = null;
try {
fos = openFileOutput("image", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
}
}
}
}
}
===========================================
03-23 13:56:07.639: WARN/System.err(5972): java.lang.IllegalArgumentException: File /mnt/sdcard/appMessage.png contains a path separator
03-23 13:56:07.639: WARN/System.err(5972): at android.app.ContextImpl.makeFilename(ContextImpl.java:1693)
03-23 13:56:07.639: WARN/System.err(5972): at android.app.ContextImpl.openFileOutput(ContextImpl.java:414)
03-23 13:56:07.639: WARN/System.err(5972): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
===========================================
可以实现的方案如下:
用InputStream is=this.getResources().openRawResource(R.drawable.icon);
再讲InputStream 转成 byte[],网上有方法
最后将这个byte[]写入到sd卡中去。 欢迎指正twins-wolf@163.com