我只是想编写一个代码来捕获图像并将该图像保存在名称为 test.jpg 的默认目录中。我的设备确实捕获了图像,但它运行测试用例的 else 部分并显示错误捕获图像。在 xml 文件中只有一个按钮并且 android:onClick 设置为处理
公共类 MainActivity 扩展 Activity {
private File imagefile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void process(View v)
{
Toast.makeText(this,"Inside the process",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imagefile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test.jpg");
Uri temp = Uri.fromFile(imagefile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,temp);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivityForResult(intent,0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0)
{
switch (resultCode){
case Activity.RESULT_OK:
if(imagefile.exists())
{
Toast.makeText(this,"File was saved at "+imagefile.getAbsolutePath(),Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this,"Error captureing image",Toast.LENGTH_SHORT).show();
}
break;
case Activity.RESULT_CANCELED:
break;
}
}
}
}