java - 如何使用 Mysql 数据库获取当前登录的用户 ID 并将该 ID 作为数据上传到 Android
问题描述
我想获取当前用户登录的用户 ID,该用户 ID 存储在共享首选项中,然后将该 ID 作为数据或带有照片的信息插入到 Mysql 数据库中的另一个表中。
我已经尝试了很多方法来获取当前登录的用户 ID,然后将其作为带有照片的数据上传。我尝试在调试器中查找错误,但调试器未显示任何预期错误。
这是我的 Java 代码
public void uploadMultipart() {
//getting name for the image
//getting the actual path of the image
String path = getPath(filePath);
//Uploading code
try {
String uploadId = UUID.randomUUID().toString();
CA_SIGNUP_GTST user = SharedPrefManager.getInstance(UploadDOC_Activity.this).getUser();
int id = user.getId();
//Creating a multi part request
new MultipartUploadRequest(UploadDOC_Activity.this, uploadId, UPLOAD_URL)
.addFileToUpload(path,"adhar_card") //Adding file
.addParameter("expid", String.valueOf(id))
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//handling the image chooser activity result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
iv_adhar.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//method to get the file path from uri
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
//Requesting permission
private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
return;
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
}
我希望会上传,但是当我运行应用程序时,它不会显示任何错误,并且用户 ID 和图像也不会插入数据库中。
解决方案
推荐阅读
- maven - Gradle MavenPublication:“尽管执行了操作,但任务尚未声明任何输出”
- graphviz - 如何在 Gephi 输入中表示节点权重?
- r - 取消引用字符串作为管道中的变量
- c# - SQL Server 数据库是否有大小限制?
- jquery - 如果 div 内的 img src 显示为“img src(unknown)”,如何隐藏 div?
- python - Python 中的 SVM 回归速度更快
- ibm-cloud-infrastructure - 关系数据类型不起作用 SoftLayer API
- javascript - 用chrome浏览器的touchstart事件替换click
- javascript - 如何在调整背景大小时获得响应式背景图像大小
- c# - C#:将许多 Json 放在 m 解决方案中以备将来使用