올해는 머신러닝이다.
android 내장메모리에 쓰기 본문
아래 방법은 SD카드 가 아닌 device 의 메모리에 이미지를 저장, 로드 , 삭제 하는 방식이다.
SD카드에 하려면 경로명을 정확히 넣어주고 메니페스트 파일에
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
이내용을 추가함을 잊지말자
1. Bitmap 저장
imgview = (ImageView)findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.comp1_16);
imgview.setImageBitmap(bm);
try{
File file = new File("test.png");
FileOutputStream fos = openFileOutput("test.png" , 0);
bm.compress(CompressFormat.PNG, 100 , fos);
fos.flush();
fos.close();
Toast.makeText(this, "file ok", Toast.LENGTH_SHORT).show();
}catch(Exception e) { Toast.makeText(this, "file error", Toast.LENGTH_SHORT).show();}
2. 저장된 Bitmap 불러오기
특이한것은 저장할때는 파일 이름만 있어도 알아서 app 의 data 폴더에 저장되지만 불러올때는
전체 패스를 다 적어줘야한다.
try{
imgview = (ImageView)findViewById(R.id.imageView1);
String imgpath = "data/data/com.test.SDCard_Ani/files/test.png";
Bitmap bm = BitmapFactory.decodeFile(imgpath);
imgview.setImageBitmap(bm);
Toast.makeText(getApplicationContext(), "load ok", Toast.LENGTH_SHORT).show();
}catch(Exception e){Toast.makeText(getApplicationContext(), "load error", Toast.LENGTH_SHORT).show();}
3. 저장된 Bitmap 파일 삭제하기
try{
File file = new File("data/data/com.test.SDCard_Ani/files/");
File[] flist = file.listFiles();
Toast.makeText(getApplicationContext(), "imgcnt = " + flist.length, Toast.LENGTH_SHORT).show();
for(int i = 0 ; i < flist.length ; i++)
{
String fname = flist[i].getName();
if(fname.equals("test.png"))
{
flist[i].delete();
}
}
}catch(Exception e){Toast.makeText(getApplicationContext(), "파일 삭제 실패 ", Toast.LENGTH_SHORT).show();}
[출처] Android Bitmap 저장 , 불러오기 , 삭제|작성자 최초산적
'Android > Tip&Tech' 카테고리의 다른 글
터치 이벤트 관련 글 (2) | 2011.04.22 |
---|---|
키패드 올려줄때 입력창 같이 올려주는 팁 (0) | 2011.04.22 |
마켓 등록 정보 (0) | 2011.04.20 |
소스 #3 - Dialog 없이 wheel 만 있는 ProgressBar 만들고 WebView 위에서 사용하기 (0) | 2011.04.15 |
Android Gallery 2D , 3D effect (0) | 2011.04.15 |