Android getExternalFilesDir在自定义首选项中未定义

Android getExternalFilesDir在自定义首选项中未定义,android,android-preferences,bitmapfactory,Android,Android Preferences,Bitmapfactory,我正在创建一个自定义首选项,其中我想显示用户名、电话号码和个人资料图片(在一个圆圈中,这还不重要)。我使用CustomPreference.class扩展首选项从首选项中获取用户名和电话号码,但我没有获取配置文件pic的路径,因为该类的getExternalFilesDir未定义。 这是我的密码: public class CustomPreference extends Preference { private TextView tvusername, tvphonenumber;

我正在创建一个自定义首选项
,其中我想显示用户名、电话号码和个人资料图片(在一个圆圈中,这还不重要)。我使用CustomPreference.class扩展首选项从首选项中获取用户名和电话号码,但我没有获取配置文件pic的路径,因为该类的
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);
    }
}

}

getExternalFilesDir()
Context
上的一个方法。在
CustomPreference
中传递一个
Context
,它从
Preference
基类继承了一个
getContext()
方法。

getExternalFilesDir()
Context
上的一个方法。在
CustomPreference
中传递一个
Context
,它从
Preference
基类继承了一个
getContext()
方法。

getExternalFilesDir()
Context
上的一个方法。在
CustomPreference
中传递一个
Context
,它从
Preference
基类继承了一个
getContext()
方法。

getExternalFilesDir()
Context
上的一个方法。在
CustomPreference
中传递了一个
Context
,它从
Preference
基类继承了一个
getContext()
方法。

getExternalFilesDir
Context
类的一个方法

因此,在类
mContext
和构造函数中创建一个全局
Context

public CustomPreference(Context context, AttributeSet attrs) 
{
  super(context, attrs);
  mContext = context
  this.setWidgetLayoutResource(R.layout.preferences_main);
}
然后可以使用
mContext
调用
getExternalFilesDir

mContext.getExternalFilesDir()

你可以打电话

getContext().getExternalFilesDir() 

getExternalFilesDir
Context
类的一种方法

因此,在类
mContext
和构造函数中创建一个全局
Context

public CustomPreference(Context context, AttributeSet attrs) 
{
  super(context, attrs);
  mContext = context
  this.setWidgetLayoutResource(R.layout.preferences_main);
}
然后可以使用
mContext
调用
getExternalFilesDir

mContext.getExternalFilesDir()

你可以打电话

getContext().getExternalFilesDir() 

getExternalFilesDir
Context
类的一种方法

因此,在类
mContext
和构造函数中创建一个全局
Context

public CustomPreference(Context context, AttributeSet attrs) 
{
  super(context, attrs);
  mContext = context
  this.setWidgetLayoutResource(R.layout.preferences_main);
}
然后可以使用
mContext
调用
getExternalFilesDir

mContext.getExternalFilesDir()

你可以打电话

getContext().getExternalFilesDir() 

getExternalFilesDir
Context
类的一种方法

因此,在类
mContext
和构造函数中创建一个全局
Context

public CustomPreference(Context context, AttributeSet attrs) 
{
  super(context, attrs);
  mContext = context
  this.setWidgetLayoutResource(R.layout.preferences_main);
}
然后可以使用
mContext
调用
getExternalFilesDir

mContext.getExternalFilesDir()

你可以打电话

getContext().getExternalFilesDir() 

太简单了,我花了一些时间却没有成功——非常感谢。@Apoorv可能的编辑。不要保存上下文。当操作系统进行清理时,它可能会导致问题,因为可能有东西链接到它。相反,保存getExternalFilesDir()的结果非常简单,我花了一些时间没有成功-非常感谢。@Apoorv可能的编辑。不要保存上下文。当操作系统进行清理时,它可能会导致问题,因为可能有东西链接到它。相反,保存getExternalFilesDir()的结果非常简单,我花了一些时间没有成功-非常感谢。@Apoorv可能的编辑。不要保存上下文。当操作系统进行清理时,它可能会导致问题,因为可能有东西链接到它。相反,保存getExternalFilesDir()的结果非常简单,我花了一些时间没有成功-非常感谢。@Apoorv可能的编辑。不要保存上下文。当操作系统进行清理时,它可能会导致问题,因为可能有东西链接到它。而是从getExternalFilesDir()保存结果