๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
APP/ANDROID

[๋ถ€์ŠคํŠธ์ฝ”์Šค] ์•ˆ๋“œ๋กœ์ด๋“œ ์นด๋ฉ”๋ผ ์ฐ์–ด์„œ ๋‚˜ํƒ€๋‚ด๊ธฐ

by mingzoo 2020. 3. 20.

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์—์„œ ์นด๋ฉ”๋ผ๋ฅผ ์ฐ์–ด ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์„๋•Œ๋Š” ๊ฐ„๋‹จํžˆ ์ธํ…ํŠธ(intent)๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด ๋”ฐ๋กœ ์นด๋ฉ”๋ผ ์„ค์ •์„ ์•ˆํ•ด๋„ ๋œ๋‹ค. ์ด๋ฏธ ์„ค์น˜๋œ ์นด๋ฉ”๋ผ ์•ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„๊ฐ€๋Šฅํ•˜๋‹ค.

์ธํ…ํŠธ ์„ค๋ช… -> https://minjoo-space.tistory.com/7

 

[๋ถ€์ŠคํŠธ์ฝ”์Šค] ์ธํ…ํŠธ(intent)๋ฅผ ์ด์šฉํ•œ ํ™”๋ฉด์ „ํ™˜

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์„ฑ์š”์†Œ 4๊ฐ€์ง€ ์•กํ‹ฐ๋น„ํ‹ฐ(Activity) ์„œ๋น„์Šค ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์ˆ˜์‹ ์ž ๋‚ด์šฉ ์ œ๊ณต์ž ์•กํ‹ฐ๋น„ํ‹ฐ(Activity) ์•กํ‹ฐ๋น„ํ‹ฐ๋Š” ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” UI๊ตฌ์„ฑ์„ ์œ„ํ•ด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ์š”์†Œ์ด๋‹ค. ํ™”๋ฉด์— UI๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด..

minjoo-space.tistory.com

 

์ „์ฒด์  ๊ตฌ์„ฑ์œผ๋กœ๋Š” ์‚ฌ์ง„์ฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด ์‚ฌ์ง„์ฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ธํ…ํŠธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , startActivityForResult๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‹œ์Šคํ…œ์œผ๋กœ ์ธํ…ํŠธ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

 

MainActivity.java

package com.example.captureintent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, "capture.jpg");
    //๊ทธ๋ƒฅ file๋กœ ์ž…๋ ฅํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.imageView);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                capture();
            }
        });
    }

    public void capture(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //์‚ฌ์ง„ ์•ฑ ๋„์šฐ๊ธฐ
        //๋ถ€๊ฐ€ ๋ฐ์ดํ„ฐ ๋„์šฐ๊ธฐ
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        //file์— ๋Œ€ํ•œ ์ •๋ณด ๋งŒ๋“ค๊ธฐ(์œ„์— file๋ณ€์ˆ˜ ์ •์˜ํ•ด๋‘ )
        startActivityForResult(intent,101);
    }

    //๋งˆ์šฐ์Šค ์šฐํด๋ฆญ-> Generate -> Override Method -> onActivityResult ํด๋ฆญ
    //intent์— ๊ทธ ์ •๋ณด๊ฐ€ ์ž…๋ ฅ๋˜์–ด ๋‚˜ํƒ€๋‚จ
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //์ •์ƒ์ ์œผ๋กœ ์‚ฌ์ง„์ด ์ฐํ˜”๋‹ค ๋‚˜ํƒ€๋‚ด์คŒ
        if(requestCode ==101 && resultCode == Activity.RESULT_OK){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
            imageView.setImageBitmap(bitmap);
        }
        //์ฐ์€ํ™”๋ฉด์„ ๊ทธ๋Œ€๋กœ ๋ณด์—ฌ์ฃผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์—†์–ด์„œ ๋น„์ •์ƒ์ข…๋ฃŒ๊ฐ€ ๋ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
        //ํฌ๊ธฐ๋ฅผ ์ค„์—ฌ์„œ ๋‚˜ํƒ€๋‚ด์คŒ
    }
}

sd์นด๋“œ์— ์ ‘๊ทผ์„ ํ•˜๋Š”๊ฑฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒํ•œ์ ‘๊ทผ์„ ํ•ด์ค€๋‹ค.

->manifest์ˆ˜์ •

AndroidManifest.xml์—

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

์„ ์ถ”๊ฐ€ํ•ด์„œ ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

 

๋˜ํ•œ, ์œ„ํ—˜๊ถŒํ•œ์ด๊ธฐ ๋•Œ๋ฌธ์— build.gradle์— ๋“ค์–ด๊ฐ€์„œ targetSdkVersion์„ 22๋กœ ๋‚ฎ์ถฐ์ค€๋‹ค.

 

 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

if (intent.resolveActivity(getPackageManager()) != null) {
  startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

 

์นด๋ฉ”๋ผ ์•ฑ์—์„œ ์ฐ์€ ์‚ฌ์ง„์„ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ๋„ฃ๊ณ , Uri๋ผ๋Š” ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Uri๋ฅผ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ง€์ •ํ•œ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด, ์นด๋ฉ”๋ผ ์•ฑ์˜ ํ™”๋ฉด์ด ๋ณด์ด๊ณ  ์‚ฌ์ง„์„ ์ฐ์„ ์ˆ˜ ์žˆ๊ฒŒ๋˜์—ˆ๋‹ค.

 

728x90