본문 바로가기

Android

이미지 캡쳐 후 메신저로 전송

경로 예
this.getExternalFilesDir(null).getPath();
Environment.getExternalStorageDirectory().toString();
/storage/emulated/0/Pictures/원하는 폴더명 등


해당 폴더가 없으면 만들고 저장

public void screenShot() {
	File isPath = new File(Constants.PATH_CAPTURE);
	if (!isPath.isDirectory())
		isPath.mkdirs();

	// 파일 지정
	String fileName = getCurrentTime("yyyyMMddHHmmss") + ".png";

	// 경로 + 파일
	File fullName = new File(Constants.PATH_CAPTURE + File.separator + fileName);

	// 캡쳐 준비
	View view = this.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap captureView = view.getDrawingCache();

	try {
		// 이미지 캡쳐
		FileOutputStream fos = new FileOutputStream(fullName);
		captureView.compress(Bitmap.CompressFormat.PNG, 100, fos);
		// 미디어 스캐닝
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fullName)));

		ToastManager.showToast(this, getString(R.string.game_capture_toast), Constants.DELAY.VERY_SHORT);

		// 게임 초기화
		stop();
		initNewGame();

		// 카카오 전송
		dialogConfirmCaptureSenMessenger(R.drawable.ic_launcher, getString(R.string.game_capture_send_title), getString(R.string.game_capture_send_content), fullName);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
}

public void screenShotMessenger(File fullName, String messenger) {
	String packageName = "";

	switch (messenger) {
	case "kakao":
		// 카카오톡 전송
		packageName = "com.kakao.talk";
		break;
	case "line":
		// 라인 전송
		packageName = "jp.naver.line.android";
		break;
	case "galary":
		// 갤러리로 보기
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.parse("file://" + fullName.getAbsolutePath()), "image/*");
		startActivity(intent);
		break;
	}

	PackageManager pm = getPackageManager();
	try {
		pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("image/png");
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fullName));
		intent.setPackage(packageName);
		startActivity(intent);
	} catch (NameNotFoundException e) {
		ToastManager.showToast(this, getString(R.string.game_capture_messenger_not_installed), Constants.DELAY.VERY_SHORT);
	}
}

'Android' 카테고리의 다른 글

ArrayList 파일로 저장 및 읽기  (0) 2015.03.17
Device별 DPI & 리소스 호출 순서  (0) 2015.02.13
상태바에 알림 생성  (0) 2015.01.19
TextView에 Marquee 효과 넣기  (0) 2015.01.07
ImageView의 이미지 교체  (0) 2015.01.07