我已将我的应用中的图片上传到服务器,并在imageview中显示该图片。当我想将imageview中的现有图像重新上传到服务器时发生错误。显示此错误:
ava.lang.NullPointerException: Attempt to get length of null array
at com.billionusers.tingting.activities.MyPictureActivity$3.onClick(MyPictureActivity.java:145)
at android.view.View.performClick(View.java:5690)
at android.view.View$PerformClick.run(View.java:22693)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6269)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
到目前为止我的代码:
uploadTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable avatar_drawable = new BitmapDrawable(getResources(), decBitmap);
if (userImage.getDrawable() != null){ //I'm trying to check if imageview has image or not
Intent editProfileIntent = null;
if (userImage.getDrawable() == avatar_drawable || imgBytes.length == 0) { // error occurs here. I'm trying to check if imageview's image is same as retrieved image
Bitmap imageViewBitmap = ((BitmapDrawable) userImage.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageViewBitmap.compress(Bitmap.CompressFormat.PNG, 10, baos);
imgBytes = baos.toByteArray();
upLoadImageBytes(imgBytes);
editProfileIntent = new Intent(MyPictureActivity.this, EditProfileActivity.class);
startActivity(editProfileIntent);
}
} else {
Snackbar.make(findViewById(android.R.id.content), "Select an Image From Camera or Gallery", Snackbar.LENGTH_LONG).show();
}
}
});
通过改造上传图片的方法,效果很好:
private void upLoadImageBytes(byte[] imgBytes) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalReq = chain.request();
Request request = originalReq.newBuilder()
.addHeader("x-access-token", key_token)
.method(originalReq.method(), originalReq.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
ImageService anInterface = retrofit.create(ImageService.class);
RequestBody imageFileBody = RequestBody.create(MediaType.parse("image/*"), imgBytes);
MultipartBody.Part partBody = MultipartBody.Part.createFormData("avatar", "avatar.jpg", imageFileBody);
Call<ImageResponse> imageCall = anInterface.upLoadImage(partBody);
imageCall.enqueue(new Callback<ImageResponse>() {
@Override
public void onResponse(Call<ImageResponse> call, retrofit2.Response<ImageResponse> response) {
Log.d(TAG, "Response body is:\t" + response.body().toString());
}
@Override
public void onFailure(Call<ImageResponse> call, Throwable t) {
Log.d(TAG, "Response error message is:\t" + t.getMessage().toString());
}
});
}
PS:我想要做的是在点击按钮时重新上传我的imageview中包含的图像。有人可以说出了什么问题吗?感谢
答案 0 :(得分:0)
你的'imgBytes'数组为null,就像异常所说的那样,你无法检查空数组的长度。
试试这个:
if (userImage.getDrawable() == avatar_drawable || imgBytes == null || imgBytes.length == 0) {
....
}