我已经实施了图片上传,但确实上传了图片,但在活动 关闭 之后,它没有在偏好设置中挑选已保存的图片( picturepath )
我已使用 Glide 上传图片,因此请尝试在该上下文中说明并使用 圈子转换 作为框架上传我的图片图像进入这个框架。
我的活动类:
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
EditText pname;
private static int RESULT_LOAD_IMAGE = 1;
private Toolbar mToolbar;
ImageView profimg;
String picturePath;
private FragmentDrawer drawerFragment;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=getSharedPreferences("profilePicture",MODE_PRIVATE);
profimg = (ImageView) findViewById(R.id.profileimg);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
pname = (EditText) findViewById(R.id.pname);
profimg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
pname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pname.setInputType(0x0000006);
pname.setCursorVisible(true);
}
});
pname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
pname.setCursorVisible(false);
}
else{
pname.setCursorVisible(false);
}
}
});
pname.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event!=null && event.getKeyCode() != KeyEvent.KEYCODE_ENTER || actionId != EditorInfo.IME_ACTION_DONE ) {
return false;
}
else if(actionId==EditorInfo.IME_ACTION_DONE || event==null || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
pname.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(pname.getWindowToken(), 0);
return false;
}
return false;
}
});
if(sp.getString("dp" ,"").equals(picturePath)){
Glide.with(this).load(picturePath)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(profimg);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
Glide.with(this).load(picturePath)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(profimg);
sp.edit().putString("dp", picturePath).apply();
}
}
答案 0 :(得分:0)
我最后解决了自己的问题,这就是答案。
首先声明全球:SharedPreferences sp;
现在在onCreate方法中执行此操作:
sp=getSharedPreferences("profilePicture",MODE_PRIVATE);
boolean path =sp.contains("dp");
if(path) {
String value=sp.getString("dp",picturePath);
Glide.with(this).load(value)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(profimg);
}
onActivityResult中的在使用滑动
加载图片后执行此操作sp.edit().putString("dp", picturePath).apply();
每次上传新图片时都会保存您的图片并重新启动您的应用,此图片会一直存在,直到您上传另一张图片,就这样了。