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()保存结果