현재 View 클래스에 보여지는 화면을 파일로 저장하는 클래스다.
View를 상속 받아 만든 클래스 ImageView, WebView 등 클래스를 이용할 수 있다.
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Picture; import android.graphics.drawable.PictureDrawable; import android.content.Context; import android.view.View; import android.widget.ImageView; import android.util.Log;
public class ImageUtil {
private static final String LOGTAG = "ImageUtil";
/*
* Picture를 Bitmap 파일로 저장
* viw : View ...
* fn : 파일 이름
* return : 성공 유무
*/
public static boolean PictureSaveToBitmapFile(View viw, final String fn )
{
boolean bRes = false;
if( viw == null && fn == null && fn.length() < 1 )
return bRes;
viw.setDrawingCacheEnabled(true);
Bitmap bmp = viw.getDrawingCache();
if( bmp == null )
return bRes;
File file = new File( fn );
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Log.e(LOGTAG, e.getMessage());
return bRes;
}
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
try {
fOut.flush();
fOut.close();
bRes = true;
} catch (IOException e) {
Log.e(LOGTAG, e.getMessage());
return bRes;
}
return bRes;
}
/*
* Bitmap 파일을 읽어오기
* fn : 파일 이름
* return : bitmap 이미지, 실패했을 경우 null
*/
public static Bitmap BitmapLoadFromFile( final String fn )
{
Bitmap bmp = null;
try
{
bmp = BitmapFactory.decodeFile( fn );
}catch( Exception e)
{
Log.e(LOGTAG, e.getMessage());
}
return bmp;
}
}
|
사용 예제
레이아웃 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="Copy"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<WebView
android:id="@+id/WebView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
|
구현 부
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Picture; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.PictureDrawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast;
public class WebViewCopy extends Activity {
private Button mBtnCopy = null;
private WebView mSource = null;
private ImageView mImgView = null;
private static final String LOGTAG = "UtimeLog";
private static final String PIC_FILE_ = "PicDump.dat";
private final static String PATH = "/sdcard/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtnCopy = (Button )findViewById(R.id.Button01);
mSource = (WebView )findViewById(R.id.WebView01);
mImgView = (ImageView)findViewById(R.id.ImageView01);
mBtnCopy.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
OnImageCopyEvent( v );
}
});
initWeb();
}
private void WriteLogMessage( final String sLog )
{
Log.i(LOGTAG, sLog);
Toast.makeText( getApplicationContext(), sLog, Toast.LENGTH_SHORT).show();
}
private void initWeb() {
mSource.setWebViewClient(new WebViewClient());
mSource.getSettings().setJavaScriptEnabled(true);
mSource.loadUrl("http://www.google.com/");
}
private void OnImageCopyEvent( View v )
{
String fn = "puhaha.dmp"; ImageUtil.PictureSaveToBitmapFile( mSource, fn); Bitmap bmp = null; bmp = ImageUtil.BitmapLoadFromFile(fn); mImgView.setImageBitmap(bmp);
}
}
|