티스토리 뷰
XML Code
<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:gravity="top|center"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/BtnSelectImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Image" />
<ImageView
android:id="@+id/ImgPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Java Code
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PIC_REQUEST = 22;
Uri cameraUri;
Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImgPhoto = (ImageView) findViewById(R.id.ImgPhoto);
BtnSelectImage = (Button) findViewById(R.id.BtnSelectImg);
BtnSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImgPhoto.setImageBitmap(photo);
} catch (Exception e) {
Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
안드로이드 sqlite 추가하기 insert 소스코드 (0) | 2021.04.08 |
---|---|
FFMpeg를 이용해 안드로이드 동영상 편집 앱 만들기 (0) | 2020.06.24 |
서버/클라이언트 소켓(SOCKET) 통신하기 (3) | 2020.06.24 |
안드로이드 원 그래프 & 꺾은선 그래프 만들기 (0) | 2020.06.24 |
안드로이드 String 변수를 숫자(int나 float)로 변환 (0) | 2020.02.04 |
댓글