Onreceive メソッドは context.startActivity(Intent.createChooser(sendIntent, "Send")); でアプリをクラッシュさせます。oncreate メソッドでは問題なく動作しますが、broadcastreceiver クラスでは動作しません。誰でも理由を知ることができますか。私は新しいアンドロイドプログラマーです。前もって感謝します。
public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
takePictureNoPreview(context);
String imagePath="test.jpeg";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body", "I Send you pic.");
sendIntent.putExtra("address", phoneNumber);
sendIntent.setType("image/png");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
// context.startActivityForResult(sendIntent, 1);
context.startActivity(Intent.createChooser(sendIntent, "Send"));
// context.startActivity(sendIntent);
}
}
}
public void takePictureNoPreview(Context context){
// open back facing camera by default
Camera myCamera= Camera.open();//openFrontFacingCameraGingerbread();
if(myCamera!=null){
try{
//set camera parameters if you want to
//...
// here, the unused surface view and holder
SurfaceView dummy=new SurfaceView(context);
myCamera.setPreviewDisplay(dummy.getHolder());
myCamera.startPreview();
myCamera.takePicture(null, null, photoCallback);
} catch (IOException e) {
e.printStackTrace();
}finally{
// myCamera.close();
}
}else{
Toast toast = Toast.makeText(context, "No front Facing camera", Toast.LENGTH_LONG);
toast.show();
}
}
Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Save the image JPEG data to the SD card
FileOutputStream fos = null;
// camera.startPreview();
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
StudentDemo.iView.setImageBitmap(bmp);
try {
fos = new FileOutputStream(dataFile,true);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*new SavePhotoTask().execute(data);
camera.startPreview();*/
}
};
}
Android マニフェスト ファイルは
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gen.stud"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="9"
/>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Demo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StudentDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.StudentDemo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>