728x90
반응형
●AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kmg.ex0414">
<!-- 카메라 권한 설정 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ex0414">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 파일 저장 설정 -->
<provider
android:authorities="com.kmg.ex0414.fileprovider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
xml이라는 Android Resource Directory만들고
그 안에 file_paths.xml 파일 만들기
●file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="external_files"
path="."/>
</paths>
●MainActivity
package com.kmg.ex0414;
public class MainActivity extends AppCompatActivity {
private ImageView img;
private Button btn_picture, btn_gallery;
private final int REQUEST_TAKE_PICTURE = 1;
private final int GET_GALLERY_IMAGE = 2;
private String currentPhotoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
btn_picture = findViewById(R.id.btn_picture);
btn_gallery = findViewById(R.id.btn_gallery);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
0);
}
//카메라 촬영기능 구현
btn_picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//startActivity(takePictureIntent);
//사진 파일명과 임시 저장경로 정의
try {
String fileName = "JPEG_"+System.currentTimeMillis();
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(fileName, ".jpg", storageDir);
currentPhotoPath = imageFile.getAbsolutePath(); //사진파일의 절대경로 저장
} catch (IOException e) {
e.printStackTrace();
}
//사진파일에 대한 정보를 Uri로 접근하여 인텐트에 저장
Uri photoURI = FileProvider.getUriForFile(MainActivity.this, "com.kmg.ex0414.fileprovider", new File(currentPhotoPath));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
//사진 촬영 후에 사진정보를 받아오기 위함
startActivityForResult(takePictureIntent, REQUEST_TAKE_PICTURE);
}
});
// 갤러리 사진 가져오기 구현
btn_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GET_GALLERY_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//사진촬영 앱에 대한 요청결과 확인
if(requestCode == REQUEST_TAKE_PICTURE && resultCode == RESULT_OK) {
Uri picturePhotoURI = Uri.fromFile(new File(currentPhotoPath));
img.setImageURI(picturePhotoURI);
}else if(requestCode == GET_GALLERY_IMAGE && resultCode == RESULT_OK){
// 갤러리에서 선택한 사진파일의 경로를 Uri객체로 생성하여 이미지뷰에 적용
Uri galleryURI = data.getData();
img.setImageURI(galleryURI);
}
}
}
반응형
'Spring' 카테고리의 다른 글
Android에서 연동 (0) | 2021.04.30 |
---|---|
LBS 활용하기 (0) | 2021.04.30 |
MONGO + MQTT로 브라우저LED제어 (0) | 2021.04.07 |
Spring 게시판 (0) | 2021.03.31 |
Spring 환경설정하기 / 툴 사용 (0) | 2021.03.29 |