如何在 Android 中调用另一个使用自定义相机应用程序拍照的活动?
我在互联网上提供的一些教程的帮助下实现了相机应用程序。
我可以拍照,将它们保存在单独的目录中,并将信息存储在我自己的数据库中。在这种情况下,我不会向用户显示任何图像预览。
现在我的要求是在拍照后显示带有“保存”和“取消”按钮的图像预览。为了实现这一目标,我使用 ImageView
和按钮创建了另一个活动。
在我的主要活动中,我正在执行以下操作。
cameraButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
........
PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] imageData, Camera c) {
Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
intent.putExtra("IMG_DATA", imageData);
startActivityForResult(intent, RESULT_OK);
camera.startPreview();
}
};
在我的 SaveOrDiscardActivity 类中,我获取数据并将其放入 ImageView 中。 我能够正确地看到预览。
现在我有以下问题。
1)从 onPictureTaken
方法调用另一个活动是个好方法吗?
2) 单击相机按钮后,需要一些可测量的时间(2 到 5 秒)才能转到另一个活动。如何解决这个问题?
提前致谢。
I implemented a Camera Application with the help of some of the tutorials available in internet.
And I am able to take pictures, saving them in a separate directory and storing the information in my own database. In this case I am not showing any preview of the Image to the user.
Now my requirement is to show a Image Preview with Save and Cancel buttons after taking the picture. For achieving this, I created another activity with ImageView
and Buttons.
And in my main Activity I am doing the following.
cameraButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
........
PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] imageData, Camera c) {
Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
intent.putExtra("IMG_DATA", imageData);
startActivityForResult(intent, RESULT_OK);
camera.startPreview();
}
};
In my SaveOrDiscardActivity
class I am getting the data and putting it in the ImageView
.
I am able to see the Preview properly.
Now I have the following questions.
1) Is it good approach to call another activity from onPictureTaken
method ?
2) After clicking on the Camera Button it is taking some measurable time(2 to 5 seconds) to go to the another activity. How to fix this issue ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下代码调用相机:
在 onActivityForresult 方法中调用 SaveOrDiscardActivity。
Invoke Camera by using following code:
Call your SaveOrDiscardActivity in onActivityForresult method.
在我对
PictureCallback
以及Activity
进行了一些研究之后,我发现您可能需要根据您的代码进行一些更改。我假设您需要使用
PictureCallback
将图片数据传递回其父活动,对吗?如果是这种情况,您可以首先使用
startActivityForResult
调用相机 Activity,然后在相机 Activity 中使用
PictureCallback 中的
如:setResult(int resultCode, Intent data)
方法为了进一步参考,请查看 Android 开发者官方网站:
捕获图片 和
活动
如果您遇到家长无法得到结果的问题,请参考这里的答案https://stackoverflow.com/a/2621390/763459
After I did a few research on
PictureCallback
as well asActivity
, I found you might need to do a little bit change based on your code.I assume that you need to use the
PictureCallback
to pass the picture data back to its parent activity, right?If that's the case, you can first call your camera activity use
startActivityForResult
Then, within your camera activity, use
setResult(int resultCode, Intent data)
method withinPictureCallback
like:For your further reference, please check Android Developer's Official Site:
Capture Picture and
Activity
And if you're facing problems that the parent can't get the result, please refer to this answer here https://stackoverflow.com/a/2621390/763459