안드로이드 스크롤 화면 캡쳐 구현
설명은 주석으로 되어있으며
setScreenCapture 함수만 호출해서 사용하면 됩니다.
manifest 파일에 퍼미션 추가
<uses-permission android:name="android.intent.action.MEDIA_MOUNTED" />
private void setScreenCapture() {
try {
View view = findViewById(R.id.캡쳐할 view 아이디);
view.buildDrawingCache();
// 긴 화면도 가져오기 위한 함수 호출
Bitmap bitmap = loadBitmapFromView(view);
if(bitmap != null) {
screenshot(bitmap);
// 저장 완료
}
else {
}
}catch (IllegalArgumentException e) {
e.printStackTrace();
Utils.toast(mContext, "사진 저장에 실패하였습니다.");
}
}
// 긴 화면도 가져오기 위한 함수
public static Bitmap loadBitmapFromView(View v)
{
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
public void screenshot(Bitmap bm) {
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/폴더명");
// 해당 폴더가 없으면 생성
if (!mFolder.exists()) {
mFolder.mkdir();
}
Random random = new Random();
String s = "파일명_" + String.valueOf( random.nextInt(99999)) + ".jpg";
File file = new File(mFolder.getAbsolutePath(), s);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 저장된 이후 바뀐 파일을 알리고
// 갤러리에서 찾기 위해 호출
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file); //out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}catch(SecurityException e) {
e.printStackTrace();
Utils.toast(mContext, "사진 저장에 실패하였습니다.");
}
}
참고 :
http://stackoverflow.com/questions/18624235/android-refreshing-the-gallery-after-saving-new-images