Android:在视图或片段中启动摄像头

时间:2017-08-02 06:17:09

标签: android android-fragments android-camera

我是android开发的初学者。我想要做的是在View Fragment中启动相机。基本上,我有一个活动,线性布局和方向垂直;包含3个视图,第一个视图是一个片段,宽度为match_parent,高度为300dps。第二个和最后一个视图是EditText字段。我有一个按钮来开始这项活动,所以我想要的是,每当我按下该按钮打开此活动时,我希望在活动的第一个片段中打开相机。下面是EditTexts的代码和图像。红色部分是片段,我希望相机从那里开始

这是XML



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_cam_calculator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nadeemahmad.smartcalculator.cam_calculator">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:text=" Back "
            android:textSize="14dp"
            android:layout_marginTop="20dp"
            android:textColor="@color/colorBlue"
            android:textAllCaps="false"
            android:background="@drawable/custom_btn_ripple_blue"
            android:id="@+id/back_frm_came"
            />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_weight="1"
            android:id="@+id/cam_fragment"
            >

        </FrameLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#fff"
            android:text="Camera Taken Text"
            android:textAlignment="center"
            android:paddingTop="40dp"
            android:textSize="24dp"
            android:textColor="#c11114"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#efefef"
            android:layout_weight="1"
            android:text="Results"
            android:paddingTop="80dp"
            android:textSize="30dp"
            android:textAlignment="center"
            android:layout_gravity="center"
            />


    </LinearLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

这是代码,但它会在另一个活动中启动相机

&#13;
&#13;
package com.example.nadeemahmad.flitv;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button ma_button;
    ImageView ma_imgView;
    static final int CAM_REQ = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ma_button = (Button)findViewById(R.id.ma_button);
        ma_imgView = (ImageView) findViewById(R.id.ma_imgView);
        ma_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              File fl = getFile();
              i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fl));
              startActivityForResult(i,CAM_REQ);
            }
        });
    }

    private File getFile(){
        File folder = new File("sdcard/Camera_app");
        if(!folder.exists()){
            folder.mkdir();
        }
        File image_file = new File(folder,"cam_image.jpg");
        return image_file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/Camera_app/cam_image.jpg";
        ma_imgView.setImageDrawable(Drawable.createFromPath(path));
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案