在我的 Android 应用程序中,当我首先点击按钮时,它应该打开相机,一旦我捕捉到图像,它应该重定向到一个新的 Activity。
但实际情况是,当我首先点击按钮时,它会重定向到新 Activity ,一旦我点击后退按钮,它就会打开相机。
我不明白我的代码有什么问题,看起来一切正常。有什么帮助吗?
public void onCameraButtonClicked()
{
camera_btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
makingDirectory(dir);
storePicture(dir);
}
}
);
}
public void makingDirectory(String dir)
{
File newdir = new File(dir);
newdir.mkdirs();
}
public void storePicture(String dir)
{
boolean flag_storePicture = false;
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try
{
newfile.createNewFile();
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
flag_storePicture = true;
Log.d("myApp", "Hiiiiii from inside");
}
catch (IOException e)
{
}
finally {
Log.d("myApp", "Hiiiiii");
Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
startActivity(camera_intent);
}
}
在我的 logcat 中,我收到了这条消息。
Skipped 62 frames! The application may be doing too much work on its main thread.
最佳答案
你也很清楚,问题是控制流,
在 try block 中,camera
应用已为您打开
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
然后紧接着 finally
被执行
finally {
Log.d("myApp", "Hiiiiii");
Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
startActivity(camera_intent);
}
现在您在相机应用程序上有了 googlemap1.Camera
Activity ,因此如果您按回车键,您将找到您的相机应用程序。
解决方案:根据您的要求,将您的 finally
block 代码连同 if
条件一起移动到 onActivityResult
以验证图像捕获任务完成。喜欢
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK){ // image captured successfully
//..code to start your other activity
Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
startActivity(camera_intent);
}else{
// .. image capture failure , user pressed cancel etc
}
}
关于android - 在我单击后退按钮之前,相机不会打开 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310093/