1.当我试图将我拍摄的照片发送到服务器时,应用程序崩溃,它显示第101行中有某些内容 这是错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.th3hokage.pfev1, PID: 25157
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
at com.example.th3hokage.pfev1.SendPic.ImageUploadToServerFunction(SendPic.java:155)
at com.example.th3hokage.pfev1.SendPic$2.onClick(SendPic.java:101)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
2.发送到服务器的方法的代码和我已经实例化ImageView的活动的顶部,这几乎是除了一些辅助方法之外的整个代码
public class SendPic extends Activity {
ImageView imageViewHolder;
EditText imageName;
ProgressDialog progressDialog ;
Intent intent ;
public static final int RequestPermissionCode = 1 ;
Bitmap bitmap;
boolean check = true;
String GetImageNameFromEditText;
String ImageNameFieldOnServer = "image_name" ;
String ImagePathFieldOnServer = "image_path" ;
String ImageUploadPathOnSever ="" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_pic);
Button CaptureImageFromCamera = (Button)findViewById(R.id.button);
imageViewHolder = (ImageView)findViewById(R.id.imageView);
Button UploadImageToServer = (Button) findViewById(R.id.button2);
imageName = (EditText)findViewById(R.id.editText);
EnableRuntimePermissionToAccessCamera();
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
public void ImageUploadToServerFunction() {
ByteArrayOutputStream byteArrayOutputStreamObject ;
byteArrayOutputStreamObject = new ByteArrayOutputStream();
bitmap = ((BitmapDrawable) ImageViewHolder.getDrawable()).getBitmap();
// Converting bitmap image to jpeg format, so by default image will upload in jpeg format.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
byteArrayOutputStreamObject);
byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();
final String ConvertImage = Base64.encodeToString(byteArrayVar,
Base64.DEFAULT);
class AsyncTaskUploadClass extends AsyncTask<Void,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog at image upload time.
progressDialog = ProgressDialog.show(SendPic.this,"Image is Uploading","Please Wait",false,false);
}
@Override
protected void onPostExecute(String string1) {
super.onPostExecute(string1);
// Dismiss the progress dialog after done uploading.
progressDialog.dismiss();
// Printing uploading success message coming from server on android app.
Toast.makeText(SendPic.this,string1,Toast.LENGTH_LONG).show();
// Setting image as transparent after done uploading.
ImageViewHolder.setImageResource(android.R.color.transparent);
}
@Override
protected String doInBackground(Void... params) {
ImageProcessClass imageProcessClass = new ImageProcessClass();
HashMap<String,String> HashMapParams = new HashMap<String,String>();
HashMapParams.put(ImageNameFieldOnServer, GetImageNameFromEditText);
HashMapParams.put(ImagePathFieldOnServer, ConvertImage);
String FinalData = imageProcessClass.ImageHttpRequest(ImageUploadPathOnSever, HashMapParams);
return FinalData;
}
}
AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
}
答案 0 :(得分:0)
您的代码
bitmap = ((BitmapDrawable) ImageViewHolder.getDrawable()).getBitmap();
应该是
ImageView someImage= (ImageView) v.findViewById(R.id.image);
bitmap = ((BitmapDrawable) someImage.getDrawable()).getBitmap();
您正在访问ImageViewHolder类的getDrawable()而不是您必须已创建的对象。
你错过了:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageViewHolder.setImageBitmap(imageBitmap);
}
}
只有在onActivityResult()中重新获得图像后,才应发送该代码以将其发送到服务器。在此之前,因为在此之前ImageView没有设置位图。
根据您的代码,REQUEST_IMAGE_CAPTURE必须等于7。 即REQUEST_IMAGE_CAPTURE = 7
修改您的Camera Capture意图,如下所示:
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}