【发布时间】:2016-07-30 11:07:17
【问题描述】:
我在fragment 中有一个button。当fragment 被调用并点击button 我希望camera 打开。我已经阅读了很多教程。但是我找不到在fragment 中实现camera 的解决方案。
这是我的java代码camera.java:
public class camera extends Fragment {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
Button btnCapture;
ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camera,
container, false);
btnCapture=(Button) rootView.findViewById(R.id.btnCapturePicture);
//imageView = (ImageView) rootView.findViewById(R.id.imageview);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
imageView.setImageBitmap(bitmap);
}
}
}
}
这是我的 camera.xml 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context="com.example.prakash.eduqueri.fragments.camera">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical">
<!-- Capture picture button
-->
<Button
android:id="@+id/btnCapturePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Picture"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp" />
这是我的日志:
FATAL EXCEPTION: main
Process: com.example.prakash.eduqueri, PID: 14511
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65636, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.prakash.eduqueri/com.example.prakash.eduqueri.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.prakash.eduqueri.camera.onActivityResult(camera.java:87)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:176)
at android.app.Activity.dispatchActivityResult(Activity.java:5437)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
新的logcat:
Unable to decode stream: java.io.FileNotFoundException: /external/images/media/24097: open failed: ENOENT (No such file or directory)
04-12 12:43:02.688 29890-29890/com.example.prakash.eduqueri I/Adreno-EGL: : EGL 1.4 高通构建:AU_LINUX_ANDROID_LNX.LA.3.5.2.2.2_RB1.04.04.04.154.004_msm8226_LNX。 LA.3.5.2.2.2_RB1__release_AU() OpenGL ES 着色器编译器版本:E031.24.00.15 建造日期:2014 年 8 月 6 日星期三 本地分支:mybranch4057433 远程分支:quic/LNX.LA.3.5.2.2.2_rb1 本地补丁:无 重建分支:AU_LINUX_ANDROID_LNX.LA.3.5.2.2.2_RB1.04.04.04.154.004 + NOTHING 04-12 12:43:02.848 29890-29890/com.example.prakash.eduqueri I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@424bdc50 time:262597724
【问题讨论】:
-
至少表明你做了什么。
-
你真的需要自己实现相机吗?如果它已经发明了,为什么还要再次发明轮子?而是启动一个cameraintent,然后将拍摄的照片取回。
-
相机打开,我收到一个错误,无法恢复活动
标签: android android-fragments android-studio