Android 使用多部分改装2上传图像
事实上,我是这个领域的新手。在图像上传过程中遇到一些问题。该过程在一段时间后自动暂停。我将在下面附上我的代码。请任何人帮助我解决这个问题 ServerResponse.java ApiClient.java ApiService.java(接口类) 清单权限Android 使用多部分改装2上传图像,android,retrofit2,image-upload,Android,Retrofit2,Image Upload,事实上,我是这个领域的新手。在图像上传过程中遇到一些问题。该过程在一段时间后自动暂停。我将在下面附上我的代码。请任何人帮助我解决这个问题 ServerResponse.java ApiClient.java ApiService.java(接口类) 清单权限 安卓显示器输出 11-0314:31:07.40413163-13163/?D/TidaProvider:TidaProvider() 11-03 14:31:07.584 13163-13163/? W/art:Android 4.1
安卓显示器输出
11-0314:31:07.40413163-13163/?D/TidaProvider:TidaProvider()
11-03 14:31:07.584 13163-13163/? W/art:Android 4.1之前的Android.graphics.PorterDuffColorFilter方法Android.support.graphics.drawable.VectorDrawableCompat.UpdatentFilter(Android.graphics.PorterDuffColorFilter,Android.content.res.ColorStateList,Android.graphics.PorterDuff$模式)将错误地重写android.graphics.drawable.drawable中的包私有方法
11-03 14:31:07.724 13163-13163/? I/ViewRootImpl:CPU渲染VSync enable=true
11-03 14:31:07.724 13163-13184/? D/OpenGLRenderer:使用EGL\u交换\u行为\u保留:true
11-03 14:31:07.734 13163-13163/? 地图集:正在验证地图。。。
11-03 14:31:07.744 13163-13163/? D/ActivityThreadInjector:clearCachedDrawables。
11-03 14:31:07.784 13163-13184/? I/Adreno EGL::EGL 1.4高通公司构建:非确定性的\u AU\u msm8916\u 32\u LA.BR.1.2.4\u RB1\u发布\u AU(Ic8ede1fb34)
OpenGL ES着色器编译器版本:E031.25.03.04
建造日期:2015年10月12日星期四
本地分行:mybranch17178083
远程分支机构:quic/LA.BR.1.2.4_rb1.30
本地修补程序:无
重建分支:没有
11-03 14:31:07.784 13163-13184/? I/OpenGLRenderer:初始化EGL,版本1.4
11-03 14:31:07.804 13163-13184/com.example.atanu.imageview D/OpenGLRenderer:启用调试模式0
11-03 14:31:07.894 13163-13163/com.example.atanu.imageview I/Timeline:Timeline:Activity\u idle id:android.os。BinderProxy@1b1dad79时间:103294123
11-03 14:31:14.813 13163-13163/com.example.atanu.imageview I/时间线:时间线:活动启动请求时间:103300687
11-03 14:31:23.323 13163-13173/com.example.atanu.imageview W/art:挂起所有线程所用时间:9.429ms
11-03 14:31:24.283 13163-13163/com.example.atanu.imageview I/Timeline:Timeline:Activity\u idle id:android.os。BinderProxy@1b1dad79时间:103310156
11-03 14:31:26.602 13163-13163/com.example.atanu.imageview I/Choreographer:跳过了34帧!应用程序可能在其主线程上做了太多工作。
11-03 14:31:27.292 13163-13163/com.example.atanu.imageview I/ViewRootImpl:CPU呈现VSync enable=true
11-03 14:31:27.372 13163-13163/com.example.atanu.imageview I/Choreographer:跳过了45帧!应用程序可能在其主线程上做了太多工作。
11-03 14:31:28.002 13163-13163/com.example.atanu.imageview I/ViewRootImpl:CPU呈现VSync enable=true
11-03 14:31:28.012 13163-13163/com.example.atanu.imageview I/Choreographer:跳过37帧!应用程序可能在其主线程上做了太多工作。
11-03 14:31:29.292 13163-13163/com.example.atanu.imageview I/Choreographer:跳过了75帧!应用程序可能在其主线程上做了太多工作。
11-03 14:31:29.342 13163-13184/com.example.atanu.imageview V/RenderScript:应用程序请求的CPU执行
11-03 14:31:29.362 13163-13184/com.example.atanu.imageview V/RenderScript:0xb83d1510启动线程,CPU 4
11-03 14:31:29.992 13163-13163/com.example.atanu.imageview I/Choreographer:跳过了40帧!应用程序可能在其主线程上做了太多工作。
如果您的问题是进度条没有被隐藏,则您没有调用progressDialog.disease()代码>在onFailure
功能中。您应该像这样上传文件
File file1 = new File(mediaPath);
int id=24;
// File file2 = new File(mediaPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
MultipartBody.Part body = MultipartBody.Part.createFormData("photo", file.getName(), requestFile);
RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"),id);
ApiService getResponse = ApiClient.getClient().create(ApiService.class);
// Call<ServerResponse> call = getResponse.uploadFile(fileToUpload1);
Call<ServerResponse> call = getResponse.uploadFile(body,requestBodyid);
编辑:您忘记为id创建请求正文。请将这些内容添加到代码中
RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"), id);
身份证是你的身份证,i.e id=24。粘贴完整的错误堆栈跟踪实际显示没有错误。成功运行。我可以从图库中选择图像文件。然后单击“上载”按钮时,“预格雷斯”按钮持续旋转,不给出任何响应。是的,我将此添加到“失败”中,是的,图像上载失败。你能告诉我这其中的错误是什么吗代码。在接口类中,我传递一个名为serial_no的int变量,将图片插入数据库中的特定行中。因此,在multipart中传递参数是否正确。stacktrace不包含任何内容。再次尝试上载,然后捕获错误stacktrace并粘贴到此处。运行期间,错误stacktrace中未显示任何内容。这是完全清楚的。但在详细信息中,它会显示与我上面提到的相同的消息。您必须启用登录改型。看到这一点:我尝试了u所说的更改。但是仍然有相同的prblm。我也尝试了,但是仍然上传失败。我应该也为此更改接口类中的smthng吗?但是在“RequestBody RequestBody id=RequestBody.create(MediaType.parse(“multipart/form data”)、String.valueOf(id))中它不将id作为int.这就是我将其解析为字符串的原因。但仍然不起作用。谢谢老板!图像上传成功…还有一件事你能告诉我,如果我想用2个图像视图上传2个图像,只需单击一个按钮,那么我应该在接口类和上传()函数中更改什么。?
public interface ApiService {
@Multipart
@POST("imageupload-1.php?")
//@POST("retrofit_example/upload_image.php")
Call<ServerResponse> uploadFile(
@Part MultipartBody.Part file1,
// @Part MultipartBody.Part file2,
//
// @Part("file") RequestBody name,
// @Part("serial_no") int serial_no);
@Part("serial_no") int serial_no);
}
public class MainActivity extends AppCompatActivity {
Button btnUpload, btnPickImage;
String mediaPath;
ImageView imgView1,imgView2;
String[] mediaColumns = { MediaStore.Video.Media._ID };
ProgressDialog progressDialog;
public static int choice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Uploading...");
btnUpload = (Button) findViewById(R.id.upload);
btnPickImage = (Button) findViewById(R.id.pick_img);
imgView1 = (ImageView) findViewById(R.id.preview1);
imgView2 = (ImageView) findViewById(R.id.preview2);
choice=0;
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadFile();
}
});
imgView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choice=1;
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 0);
}
});
imgView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choice=2;
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);
// Set the Image in ImageView for Previewing the Media
if(choice==1) {
imgView1.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();
}
/* else if(choice==2) {
imgView2.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();
} */
} else {
Toast.makeText(this, "You haven't picked Image/Video", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
// Uploading Image/Video
private void uploadFile() {
progressDialog.show();
// Map is used to multipart the file using okhttp3.RequestBody
File file1 = new File(mediaPath);
// File file2 = new File(mediaPath);
// Parsing any Media type file
RequestBody requestBody1 = RequestBody.create(MediaType.parse("image/*"), file1);
// RequestBody requestBody2 = RequestBody.create(MediaType.parse("*/*"), file2);
MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("file", file1.getName(), requestBody1);
// MultipartBody.Part fileToUpload2 = MultipartBody.Part.createFormData("file", file2.getName(), requestBody2);
RequestBody filename1 = RequestBody.create(MediaType.parse("text/plain"), file1.getName());
// RequestBody filename2 = RequestBody.create(MediaType.parse("text/plain"), file2.getName());
ApiService getResponse = ApiClient.getClient().create(ApiService.class);
// Call<ServerResponse> call = getResponse.uploadFile(fileToUpload1);
Call<ServerResponse> call = getResponse.uploadFile(fileToUpload1,24);
Toast.makeText(MainActivity.this,String.valueOf(file1),Toast.LENGTH_LONG).show();
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
ServerResponse serverResponse = response.body();
if (serverResponse != null) {
if (serverResponse.getSuccess()) {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
}
} else {
assert serverResponse != null;
Log.v("Response", serverResponse.toString());
}
progressDialog.dismiss();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
}
});
}
}
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
11-03 14:31:07.404 13163-13163/? D/TidaProvider: TidaProvider()
11-03 14:31:07.584 13163-13163/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-03 14:31:07.724 13163-13163/? I/ViewRootImpl: CPU Rendering VSync enable = true
11-03 14:31:07.724 13163-13184/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-03 14:31:07.734 13163-13163/? D/Atlas: Validating map...
11-03 14:31:07.744 13163-13163/? D/ActivityThreadInjector: clearCachedDrawables.
11-03 14:31:07.784 13163-13184/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8916_32_LA.BR.1.2.4_RB1__release_AU (Ic8ede1fb34)
OpenGL ES Shader Compiler Version: E031.25.03.04
Build Date: 12/10/15 Thu
Local Branch: mybranch17178083
Remote Branch: quic/LA.BR.1.2.4_rb1.30
Local Patches: NONE
Reconstruct Branch: NOTHING
11-03 14:31:07.784 13163-13184/? I/OpenGLRenderer: Initialized EGL, version 1.4
11-03 14:31:07.804 13163-13184/com.example.atanu.imageview D/OpenGLRenderer: Enabling debug mode 0
11-03 14:31:07.894 13163-13163/com.example.atanu.imageview I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1b1dad79 time:103294123
11-03 14:31:14.813 13163-13163/com.example.atanu.imageview I/Timeline: Timeline: Activity_launch_request time:103300687
11-03 14:31:23.323 13163-13173/com.example.atanu.imageview W/art: Suspending all threads took: 9.429ms
11-03 14:31:24.283 13163-13163/com.example.atanu.imageview I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1b1dad79 time:103310156
11-03 14:31:26.602 13163-13163/com.example.atanu.imageview I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
11-03 14:31:27.292 13163-13163/com.example.atanu.imageview I/ViewRootImpl: CPU Rendering VSync enable = true
11-03 14:31:27.372 13163-13163/com.example.atanu.imageview I/Choreographer: Skipped 45 frames! The application may be doing too much work on its main thread.
11-03 14:31:28.002 13163-13163/com.example.atanu.imageview I/ViewRootImpl: CPU Rendering VSync enable = true
11-03 14:31:28.012 13163-13163/com.example.atanu.imageview I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
11-03 14:31:29.292 13163-13163/com.example.atanu.imageview I/Choreographer: Skipped 75 frames! The application may be doing too much work on its main thread.
11-03 14:31:29.342 13163-13184/com.example.atanu.imageview V/RenderScript: Application requested CPU execution
11-03 14:31:29.362 13163-13184/com.example.atanu.imageview V/RenderScript: 0xb83d1510 Launching thread(s), CPUs 4
11-03 14:31:29.992 13163-13163/com.example.atanu.imageview I/Choreographer: Skipped 40 frames! The application may be doing too much work on its main thread.
File file1 = new File(mediaPath);
int id=24;
// File file2 = new File(mediaPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
MultipartBody.Part body = MultipartBody.Part.createFormData("photo", file.getName(), requestFile);
RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"),id);
ApiService getResponse = ApiClient.getClient().create(ApiService.class);
// Call<ServerResponse> call = getResponse.uploadFile(fileToUpload1);
Call<ServerResponse> call = getResponse.uploadFile(body,requestBodyid);
RequestBody requestBody1 = RequestBody.create(MediaType.parse("image/*"), file1);
RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"), id);