androidmainfest.xml에 권한 추가

 

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

 

 

 

 

 

//클릭이벤트에서 캡처 실행

 

@Override

public void onClick(View arg0) {

    Bitmap screenshot    = CaptureUtil.captureScreen(findViewById(android.R.id.content), getApplicationContext());

    if(screenshot != null){

        try {

            CaptureUtil.saveImage(screenshot);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

 

 

 

//캡쳐 원문

package co.kr.kookai.util;

 

import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.net.Uri;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.widget.Toast;

 

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Calendar;

 

/**

 * Created by kookai on 2015-07-07.

 */

public class CaptureUtil {

    static final String PICFOLDER = "DCIM/kookai"

save_context    =   _context;

        Bitmap screenshot = null;

        try {

 

            if(v!=null) {

 

                screenshot = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(screenshot);

                v.draw(canvas);

            }

 

        }catch (Exception e){

            Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());

        }

 

        return screenshot;

    }

 

    public static void saveImage(Bitmap bitmap) throws IOException {

        mRootPath   =   Environment.getExternalStorageDirectory() + File.separator + PICFOLDER;

 

        File fRoot = new File(mRootPath);

        if(fRoot.exists() == false) {

            if(fRoot.mkdir() == false) {

                Toast.makeText(save_context, "사진을 저장할 폴더가 없습니다.", Toast.LENGTH_SHORT).show();

                return;

            }

        }

 

        Calendar calendar = Calendar.getInstance();

        String FileName = String.format("DG%04d%02d%02d-%02d%02d%02d.jpg",

                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,

                calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),

                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));

 

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);

        //File f = new File(Environment.getExternalStorageDirectory() + File.separator + PICFOLDER+"test.png");

 

        String file_path    =   mRootPath + File.separator +FileName;

 

        File f = new File(file_path);

        f.createNewFile();

        FileOutputStream fo = new FileOutputStream(f);

        fo.write(bytes.toByteArray());

        fo.flush();

        fo.close();

 

        //파일을 갤러리에 저장

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        Uri uri = Uri.parse("file://" + file_path);

        intent.setData(uri);

        save_context.sendBroadcast(intent);

 

        Toast.makeText(save_context, "SAVE OK ", Toast.LENGTH_SHORT).show();

 

    }

}

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3136
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2372
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4025
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4036
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3852
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3827
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4107
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3986
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4106
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4022
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4181
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4225
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4083
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4019
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4114
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3965