gpt4 book ai didi

java - 我的共享首选项无法正确共享

转载 作者:行者123 更新时间:2023-12-02 01:35:58 26 4
gpt4 key购买 nike

我有一个应用程序,您可以将图像上传到我公司的服务器因此,用户输入他们的登录详细信息、电子邮件、密码和 clientID(4 位代码)(在 LoginActivity.java 中),然后必须将此信息传递给所有其他 Activity ,然后使用此传递的信息来构建 URL。现在我遇到的问题是 Sharedprefrences 无法正确共享...它们要么在 url 上显示为 NULL,要么只是“电子邮件”或“密码”信息已正确保存在登录 Activity 中,但是当我尝试将其传递给其他 Activity 时,它失败了

在此处登录 Activity 我保存首选项

public class LoginActivity extends AppCompatActivity implements TextWatcher {


SharedPreferences MyPrefs;
Intent intent;
SharedPreferences.Editor editor;
public static final String PREF_NAME= "MYPREFS";
public static final String ID = "ClientID" ;
public static final String EMAIL = "username" ;
public static final String PASS = "password";

EditText email, password, id;

@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button buttonOne=findViewById(R.id.button);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {



Intent activity2Intent=new Intent(getApplicationContext(), MainActivity.class);
startActivity(activity2Intent);

}
});

MyPrefs= getSharedPreferences(PREF_NAME, 0);
editor = MyPrefs.edit();


email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);

email.setText(MyPrefs.getString(EMAIL,"username"));
password.setText(MyPrefs.getString(PASS,"password"));
id.setText(MyPrefs.getString(ID, "id"));
email.addTextChangedListener(this);
password.addTextChangedListener(this);
id.addTextChangedListener(this);

MyPrefs =getSharedPreferences(EMAIL,0);
MyPrefs =getSharedPreferences(ID,0);
MyPrefs =getSharedPreferences(PASS,0);
intent = new Intent(LoginActivity.this,CameraActivity.class);


}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
managePrefs();
}

@Override
public void afterTextChanged(Editable editable) {
managePrefs();

}


private void managePrefs(){
SharedPreferences.Editor editor =MyPrefs.edit();

editor.putString(EMAIL, email.getText().toString().trim());
editor.putString(PASS, password.getText().toString().trim());
editor.putString(ID, id.getText().toString().trim());

editor.apply();

}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
}

相机 Activity ,这是共享首选项必须传递到的位置

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

private final int PICK_IMAGE=12345;
private final int REQUEST_CAMERA=6352;
private static final int REQUEST_CAMERA_ACCESS_PERMISSION=5674;
private Bitmap bitmap;

String myURL;
String email;
String clientId;
String pwd;




private ImageView imageView;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);

我的调用共享首选项的代码

SharedPreferences sharedPreferences = getSharedPreferences(LoginActivity.PREF_NAME, 0);
email = sharedPreferences.getString(LoginActivity.EMAIL, "username");
clientId = sharedPreferences.getString(LoginActivity.ID, "id");
pwd = sharedPreferences.getString(LoginActivity.PASS, "password");


imageView=findViewById(R.id.imageView);
Button fromCamera=findViewById(R.id.fromCamera);
Button fromGallery=findViewById(R.id.fromGallery);
Button upload=findViewById(R.id.upload);
upload.setOnClickListener(this);
fromCamera.setOnClickListener(this);
fromGallery.setOnClickListener(this);
}

@Override
public void onClick(View view) {



switch (view.getId()) {
case R.id.fromCamera:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_ACCESS_PERMISSION);
} else {
getImageFromCamera();
}
break;
case R.id.fromGallery:
getImageFromGallery();
break;
case R.id.upload:
if (bitmap != null)
uploadImageToServer();
break;

}


}


private void uploadImageToServer() {
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
String currentTimeStamp = dateFormat.format(new Date());
final ProgressDialog pd=new ProgressDialog(CameraActivity.this);
pd.setMessage("Uploading, Please Wait....");
pd.show();
Intent intent = getIntent();
String Item= intent.getStringExtra("Spinner");
String Item2= intent.getStringExtra("Spinner2");


Uri.Builder builder=new Uri.Builder();
builder.scheme("https")
.authority("www.smartpractice.co.za")
.appendPath("files-upload-ruben.asp")
.appendQueryParameter("MyForm", "Yes")
.appendQueryParameter("ClientID",clientId)
.appendQueryParameter("Username", email)
.appendQueryParameter("Pwd", pwd)
.appendQueryParameter("category",Item )
.appendQueryParameter("client",Item2 );
myURL=builder.build().toString();
Toast toast = Toast.makeText(CameraActivity.this, myURL , Toast.LENGTH_LONG);
toast.show();

File imageFile=persistImage(bitmap,currentTimeStamp);

Ion.with(this)
.load(myURL)
.uploadProgressDialog(pd)
.setMultipartFile("SP-LOG", "image/jpeg", imageFile)


.asString()


.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
pd.cancel();
Toast.makeText(getApplicationContext(),"Uploaded",Toast.LENGTH_SHORT).show();

}
});

}

private File persistImage(Bitmap bitmap, String name) {
File filesDir=getApplicationContext().getFilesDir();
File imageFile=new File(filesDir, name + ".jpg");

OutputStream os;
try {
os=new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}

return imageFile;
}


private void getImageFromCamera() {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}

private void getImageFromGallery() {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
try {
InputStream inputStream=getContentResolver().openInputStream(data.getData());
bitmap=BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}
} else if (requestCode == REQUEST_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
Bundle extras=data.getExtras();
bitmap=(Bitmap) extras.get("data");
imageView.setImageBitmap(bitmap);
}
}
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_ACCESS_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getImageFromCamera();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

}
}
}

最佳答案

要将数据存储在共享首选项中,请执行以下操作:

private SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putString("email", email);
editor.putString("ID", id);
editor.putString("Pass", password);
editor.apply();

所以当你写 editor.putString("email", email); 时,我会给你一些解释。它告诉编辑器将您的电子邮件放在关键的“电子邮件”旁边。

现在,如果您想读回这些值,请执行以下操作:

String email = getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("email", "");
String ID= getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("ID", "");
String password= getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("Pass", "");

如果您不明白任何内容,请告诉我。

关于java - 我的共享首选项无法正确共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57519667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com