마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 Drawable 오브젝트를 sharedpreferences에 저장하기..

0 추천

Drawable을 어플이 onStop() 될 때 저장해서, 어플을 껏다가 켜서 onCreate할 때

다시 imageView를 set해주려 합니다..

그런데 sharedpreferences에는 String, Int, float 등 값들은 저장 가능하지만,.

Drawable 형태는 저장이 불가능하더라구요..

그래서 Gson 라이브러리를 이용해서 저장하려 했습니다.

 

  @Override
    public void onStop() {

        super.onStop();

        Gson gson = new Gson();  //먼저 Gson객체를 생성한다. ​

        String weatherDrawable = gson.toJson(weatherIconDrawable);
       //weatherIconDrawable이 저장하고 싶은 Drawable 입니다...

        // Activity 가 종료되기 전에 저장한다

        // SharedPreferences 에 설정값(특별히 기억해야할 사용자 값)을 저장하기
        SharedPreferences sf = this.getActivity().getSharedPreferences("weather", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sf.edit();//저장하려면 editor가 필요
        //String str = et.getText().toString(); // 사용자가 입력한 값
        editor.putString("weatherIcon", weatherDrawable); // 입력

        editor.commit(); // 파일에 최종 반영함\
    }

 

 

문제는 어플이 잘 돌아가다가 onStop()이 되서 위의 코드를 실행하려하면

멈춰버립니다.

11-11 00:54:17.729 12592-12602/com.example.ileem.tiiu I/art: Background sticky concurrent mark sweep GC freed 328736(5MB) AllocSpace objects, 156(11MB) LOS objects, 40% free, 21MB/36MB, paused 6.631ms total 89.888ms

11-11 00:54:18.329 12592-12602/com.example.ileem.tiiu I/art: Background partial concurrent mark sweep GC freed 427490(7MB) AllocSpace objects, 195(15MB) LOS objects, 40% free, 23MB/39MB, paused 7.468ms total 258.925ms

11-11 00:54:18.799 12592-12602/com.example.ileem.tiiu I/art: Background sticky concurrent mark sweep GC freed 323389(5MB) AllocSpace objects, 142(11MB) LOS objects, 36% free, 24MB/39MB, paused 8.467ms total 108.234ms

11-11 00:54:19.429 12592-12598/com.example.ileem.tiiu W/art: Suspending all threads took: 15.267ms

11-11 00:54:19.439 12592-12602/com.example.ileem.tiiu I/art: Background partial concurrent mark sweep GC freed 448120(7MB) AllocSpace objects, 187(16MB) LOS objects, 37% free, 26MB/42MB, paused 7.693ms total 289.729ms

11-11 00:54:19.889 12592-12602/com.example.ileem.tiiu I/art: Background sticky concurrent mark sweep GC freed 326431(5MB) AllocSpace objects, 130(11MB) LOS objects, 33% free, 28MB/42MB, paused 6.361ms total 97.721ms

11-11 00:54:20.549 12592-12598/com.example.ileem.tiiu W/art: Suspending all threads took: 133.463ms 11-11 00:54:20.559 12592-12602/com.example.ileem.tiiu I/art: Background partial concurrent mark sweep GC freed 404291(6MB) AllocSpace objects, 162(15MB) LOS objects, 35% free, 28MB/44MB, paused 6.506ms total 331.491ms

로그엔 이렇게 쭉 계속 올라오다가 앱이 튕깁니다..

구글링해보니, Gson을 잘못사용해서 그런거 같다고 하던데,

올바른 Gson사용법이나, 다르게 Drawable을 저장하는 방법을 알려주실분ㅠ

똑돌이 (530 포인트) 님이 2016년 11월 11일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...