package map.demo;
public class MapDemoActivity extends Activity {
Button capture;
ImageView image;
int cameracode=100;
Bitmap bm;
Boolean result;
FileOutputStream fos;
File sd;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
capture=(Button)findViewById(R.id.capture);
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
image=(ImageView)findViewById(R.id.image);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameracode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==100)
{
bm=(Bitmap) data.getExtras().get("data");
image.setImageBitmap(bm);
image.setDrawingCacheEnabled(true);
bm = image.getDrawingCache();
if(bm==null)
{
Toast.makeText(getApplicationContext(), "Image is null", 1000).show();
}
else
{
try {
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
result=bm.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我正在执行上述代码以从相机捕获图像并将图像设置为图像视图并将图像转换为 jpeg,但我没有得到图像,它显示为空。即在图像捕获后我的代码 bm=null 中。但图像视图显示默认情况下用于相机的图像(我正在使用模拟器捕获图像)。