안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2011년 1월 18일 화요일

[안드로이드] SD카드에서 가져 온 이미지로 배경화면 만들기(android make background from sd card image)

package skin.file;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class FileManager {
private final static String PATH = "path.txt";
private Context ctx;
public FileManager(){

}
public void initFiles(Context theContext) {
ctx = theContext;
}
public void setBackground(LinearLayout linear){
String iPath = readImagePath();
   /** 완성된 이미지 보여주기  */
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;

Bitmap bm = BitmapFactory.decodeFile(iPath, bfo);

//bitmap을 drawable로
Drawable d =new BitmapDrawable(bm);
linear.setBackgroundDrawable(d);
}
public void setBackground(RelativeLayout linear){
String iPath = readImagePath();
   /** 완성된 이미지 보여주기  */
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;

Bitmap bm = BitmapFactory.decodeFile(iPath, bfo);

//bitmap을 drawable로
Drawable d =new BitmapDrawable(bm);
linear.setBackgroundDrawable(d);
}
public void writeImagePath(String imgPath)
{
try{
// Other = createPackageContext("skin.test",Context.CONTEXT_IGNORE_SECURITY);
FileOutputStream fOut = ctx.openFileOutput(PATH, Context.MODE_PRIVATE);
OutputStreamWriter out1 = new OutputStreamWriter(fOut);
out1.write(imgPath);
out1.close();
}
catch(Throwable t){
Toast
.makeText(ctx, "Exception: "+t.toString(), 5000)
.show();
}
}
public String readImagePath()
{
StringBuffer buf=new StringBuffer();
try{
// Other = createPackageContext("skin.test",Context.CONTEXT_IGNORE_SECURITY);
InputStream mIS1 = ctx.openFileInput(PATH);
InputStreamReader tmp = new InputStreamReader(mIS1);
BufferedReader reader=new BufferedReader(tmp);

String str;


while ((str = reader.readLine()) != null) {
buf.append(str);
}

mIS1.close();

}catch (java.io.FileNotFoundException e) {
Log.i("FileNotFoundException","");
}
catch (Throwable t) {
Toast
.makeText(ctx, "Exception: "+t.toString(), 5000)
.show();
}
String rString = null;
if(buf.length() > 0)
rString = buf.toString();

return rString;
}
}
writeImage의 Parameter에 sdcard의 이미지 경로를 넘겨주면 그 값을 path.txt 에 저장하여 후에 배경화면으로 쓰일 때 이용하는 소스입니다.

 안 되시거나 궁금한 점 있으면 댓글 달아 주세요.

댓글 없음:

댓글 쓰기