아래 방법은 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();}
    

+ Recent posts