Projemizi olusturup activity_main.xml layoutumuza iki tane button, imageView ve videoView ekleyecegim. Butonlardan birisi fotograf cekmek, digeri ise video cekmek icin olacak. ImageView resimleri gostermek, videoView ise cekilen videoyu gostermek icin kullanilacak.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.gamze.cameraandvideoexample.MainActivity"> <Button android:id="@+id/imageButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Picture" /> <Button android:id="@+id/videoButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Capture Video" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <VideoView android:id="@+id/videoView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Simdi ise MainActivity.java dosyamiza gerekli kodlari ekleyelim.
package com.example.gamze.cameraandvideoexample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private VideoView videoView;
private static final int IMAGE_REQUEST =1;
private static final int VIDEO_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takePicButton = (Button) findViewById(R.id.imageButton);
takePicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView = ((ImageView) findViewById(R.id.imageView));
Intent pic_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pic_intent, IMAGE_REQUEST);
}
});
Button captureVideoButton = ((Button) findViewById(R.id.videoButton));
captureVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
videoView = (VideoView)findViewById(R.id.videoView);
Intent video_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(video_intent,VIDEO_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK) return;
else if(requestCode == IMAGE_REQUEST){
Bitmap image = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(image);
}
else if(requestCode == VIDEO_REQUEST){
videoView.setVideoURI(data.getData());
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}
}
}
Son durumda ekranimiz nasil gorunuyor ona bakalim.
Hiç yorum yok:
Yorum Gönder