마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

공유하기 버튼을 누르면 캡쳐후 공유하려는 기능 구현

0 추천
    Bitmap bitmap;


    private void takeScreenshot() {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            openScreenshot(imageFile);
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
    }
    private void openScreenshot(File imageFile) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(imageFile);
        intent.setDataAndType(uri, "image/*");
        startActivity(intent);
    }


case R.id.shareicon:
//
//
                takeScreenshot();

                Bitmap icon = bitmap;
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
                try {
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
                startActivity(Intent.createChooser(share, "Share Image"));

        }

 

bitmap이라는 Bitmap 변수를 만들고 캡쳐해서 저장하는 함수를 만든다음 클릭시 캡쳐하는 함수를 실행해서 이 캡쳐한 이미지를 가지고 인텐트를 이용해서 공유하려는 기능을 구현하려는데 잘안되네요.

공유하기 버튼 클릭시 이미지가 없는 상태로 공유하기가 뜨는데, 보내게되면 사진 을 찾을수가 없다고 나오네요. 로그캣 오류도 안나서 어디가 문제인지 잘모르겠네요..

 

개수이 (260 포인트) 님이 2015년 11월 17일 질문
개수이님이 2015년 11월 18일 수정

1개의 답변

0 추천
http://blog.naver.com/hg1286/220541645364

 

참고바랍니다.

 

같은 현상을 겪어서 저장해서 보내는 방식으로 전환했습니다. 파일이름이 계속 같은 것으로 저장하므로 크게 메모리를 차지하지 않습니다.
Autopro (1,780 포인트) 님이 2015년 11월 19일 답변
...