2014-08-28 1 views
0

사용자 정의 기본 설정 <com.myproject.CustomPreference android:layout="@layout/preferences_main" />을 작성하여 사용자 이름, 전화 번호 및 프로필 그림 (아직 중요하지 않은 원 안에 있음)을 표시하고자합니다. Preference를 확장 한 CustomPreference.class를 사용하여 기본 설정에서 사용자 이름과 전화 번호를 얻을 수 있었지만 getExternalFilesDir은이 클래스에 대해 정의되지 않았기 때문에 프로필 사진에 대한 경로를 얻지 못합니다.getExternalFilesDir이 사용자 정의 기본 설정에서 정의되지 않았습니다.

public class CustomPreference extends Preference { 
    private TextView tvusername, tvphonenumber; 
    private ImageView profilepic_profile; 

public CustomPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setWidgetLayoutResource(R.layout.preferences_main); 
} 

@Override 
protected void onBindView(View view) { 
    super.onBindView(view); 
    tvusername = (TextView) view.findViewById(R.id.tvusername); 
    tvphonenumber = (TextView) view.findViewById(R.id.tvphonenumber); 
    profilepic_profile = (ImageView) view 
      .findViewById(R.id.profilepic_profile); 

    final SharedPreferences prefs = getSharedPreferences(); 

    // entering preference username in text view 
    String username = prefs.getString("username", null); 
    tvusername.setText(username); 

    // entering preference phonenumber in text view 
    String phonenumber = prefs.getString("phonenumber", null); 
    tvphonenumber.setText(phonenumber); 

    // Show currently saved profile pic in imageview 

    String fname = "profile.png"; 
    Bitmap photo2 = BitmapFactory.decodeFile(getExternalFilesDir(null) 
      .getAbsolutePath() + "/images/" + fname); 
    if (photo2 != null) { 
     GraphicsUtil graphicUtil2 = new GraphicsUtil(); 
     profilepic_profile.setImageBitmap(graphicUtil2.getCircleBitmap(
       photo2, 16)); 
    } else { 
     // profilepic_profile.setBackground(profile_pic_big); 
    } 
} 

} 

답변

1

getExternalFilesDirContext 클래스의 메소드입니다.

그래서 getExternalFilesDir

mContext.getExternalFilesDir() 

또는

당신이

를 호출 할 수 있습니다

public CustomPreference(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    mContext = context 
    this.setWidgetLayoutResource(R.layout.preferences_main); 
} 

를 수행 한 다음 전화를 mContext를 사용할 수있는 클래스 mContext에 당신의 생성자에서 글로벌 Context를 만들

getContext().getExternalFilesDir() 
+0

너무 쉽고 시간이 많이 걸리지는 않았습니다. 덕분에 많은 성공을 거두었습니다. – sascha

+0

@Apoorv 가능 편집. 컨텍스트를 저장하지 마십시오. 아마 뭔가가 연결되어 있기 때문에 os가 정리 작업을 수행 할 때 문제가 발생할 수 있습니다. 대신 getExternalFilesDir()에서 결과를 저장하십시오. – danny117

2

getExternalFilesDir()Context의 방법 : 다음은 내 코드입니다. CustomPreferenceContext이 전달되고 Preference 기본 클래스에서 getContext() 메서드를 상속받습니다.