Android 无法将图片从库发送到服务器
我正试图将所选图像从库发送到服务器,但它没有发送 我得到Android 无法将图片从库发送到服务器,android,Android,我正试图将所选图像从库发送到服务器,但它没有发送 我得到无法上传图像响应 但我能够发送捕获的图像,代码是相似的。我必须对库部分进行哪些调整 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT);
无法上传图像响应
但我能够发送捕获的图像,代码是相似的。我必须对库部分进行哪些调整
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST);
来自画廊
if (requestCode == GALLERY_REQUEST) {
if (resultCode == RESULT_OK) {
if (attachLayDialog != null && attachLayDialog.isShowing()) {
attachLayDialog.dismiss();
}
camera_preview_lay.setVisibility(View.VISIBLE);
Uri uri = data.getData();
//file = new File(uri.getPath());
file = new File(getRealPathFromURI(this, uri));
camera_preview_iv.setImageURI(uri);
camera_preview_send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress_bar.setVisibility(View.VISIBLE);
progress_bar_percentage.setVisibility(View.VISIBLE);
AndroidNetworking.upload(URLs.C_S_I)
.addMultipartFile("image", file)
.setPriority(Priority.HIGH)
.build()
.setUploadProgressListener((bytesUploaded, totalBytes) -> {
float progress = (float) bytesUploaded / totalBytes * 100;
progress_bar.setProgress((int) progress);
progress_bar_percentage.setText(String.format("%.0f%%",progress));
})
.getAsString(new StringRequestListener() {
@Override
public void onResponse(String response) {
Log.i("uploadimg", response);
try {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
String message = jsonObject.getString("message");
if (status == 0) {
Toast.makeText(this, "Unable to upload Image: " + message, Toast.LENGTH_SHORT).show();
} else { //success
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
camera_preview_lay.setVisibility(View.GONE);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
e.printStackTrace();
Toast.makeText(this, "Parsing Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(ANError anError) {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
anError.printStackTrace();
Toast.makeText(this, "Error uploading Image", Toast.LENGTH_SHORT).show();
}
});
}
});
拍摄图像
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (attachLayDialog != null && attachLayDialog.isShowing()) {
attachLayDialog.dismiss();
}
camera_preview_lay.setVisibility(View.VISIBLE);
Glide.with(this).load(currentImagePath).into(camera_preview_iv);
File orgiFile = new File(currentImagePath);
camera_preview_send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress_bar.setVisibility(View.VISIBLE);
progress_bar_percentage.setVisibility(View.VISIBLE);
AndroidNetworking.upload(URLs.C_S_I)
.addMultipartFile("image", orgiFile)
.setPriority(Priority.HIGH)
.build()
.setUploadProgressListener((bytesUploaded, totalBytes) -> {
float progress = (float) bytesUploaded / totalBytes * 100;
progress_bar.setProgress((int) progress);
progress_bar_percentage.setText(String.format("%.0f%%",progress));
})
.getAsString(new StringRequestListener() {
@Override
public void onResponse(String response) {
Log.i("uploadimg", response);
try {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
String message = jsonObject.getString("message");
if (status == 0) {
Toast.makeText(this, "Unable to upload Image: " + message, Toast.LENGTH_SHORT).show();
} else { //success
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
camera_preview_lay.setVisibility(View.GONE);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
e.printStackTrace();
Toast.makeText(this, "Parsing Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(ANError anError) {
progress_bar.setVisibility(View.GONE);
progress_bar_percentage.setVisibility(View.GONE);
anError.printStackTrace();
Toast.makeText(this, "Error uploading Image", Toast.LENGTH_SHORT).show();
}
});
}
});