Java 从“多媒体资料”中选择图像后,我的应用程序强制关闭

Java 从“多媒体资料”中选择图像后,我的应用程序强制关闭,java,android,image-upload,Java,Android,Image Upload,我正在尝试在我的应用程序中实现上载功能,但在画廊中选择图像后,我的应用程序强制关闭。这是我的密码 AddCatergory.java public class AddCategory extends AppCompatActivity implements View.OnClickListener { private Button buttonChoose; private Button buttonUpload; private ImageView imageView; private Ed

我正在尝试在我的应用程序中实现上载功能,但在画廊中选择图像后,我的应用程序强制关闭。这是我的密码

AddCatergory.java

public class AddCategory extends AppCompatActivity implements View.OnClickListener {

private Button buttonChoose;
private Button buttonUpload;
private ImageView imageView;
private EditText editTextName;
private Bitmap bitmap;
private int PICK_IMAGE_REQUEST = 1;
private String UPLOAD_URL ="http://knyjayjay.16mb.com/products/upload.php";
private String KEY_IMAGE = "image";
private String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_category);
    buttonUpload = (Button) findViewById(R.id.btn_addCat);
    buttonChoose = (Button) findViewById(R.id.chooseimgbtn);
    buttonChoose = (Button) findViewById(R.id.chooseimgbtn);
    buttonUpload = (Button) findViewById(R.id.btn_addCat);
    editTextName = (EditText) findViewById(R.id.input_name);
    imageView  = (ImageView) findViewById(R.id.imageView);
    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);
}
public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}
private void uploadImage(){
    //Showing the progress dialog
    final ProgressDialog loading = ProgressDialog.show(this,"Uploading","Please wait...",false,false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Disimissing the progress dialog
                    loading.dismiss();
                    //Showing toast message of the response
                    Toast.makeText(AddCategory.this, s , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //Dismissing the progress dialog
                    loading.dismiss();

                    //Showing toast
                    Toast.makeText(AddCategory.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            //Converting Bitmap to String
            String image = getStringImage(bitmap);
            //Getting Image Name
            String name = editTextName.getText().toString();
            //Creating parameters
            Map<String,String> params = new Hashtable<String, String>();
            //Adding parameters
            params.put(KEY_IMAGE, image);
            params.put(KEY_NAME, name);

            //returning parameters
            return params;
        }
    };
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding request to the queue
    requestQueue.add(stringRequest);
}
private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@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) {
        Uri filePath = data.getData();
        try {
            //Getting the Bitmap from Gallery
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //Setting the Bitmap to ImageView
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@Override
public void onClick(View v) {

    if(v == buttonChoose){
        showFileChooser();
    }

    if(v == buttonUpload){
        uploadImage();
    }
}  }

我知道有点晚了但万一有人需要

您应该使用输入流来获取Uri,类似这样

InputStream is=getContentResolver().openInputStream(data.getData())

然后

bitmap = BitmapFactory.decodeStream(is);
它对我很好

更改此行

bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),filePath);
imageView.setImageBitmap(bitmap);
使用滑翔

Glide.with(this)
                .load(uri)
                .dontAnimate()
                .placeholder(Drawable)
                .into(ImageView);

第148行是哪一行代码?这里似乎有一个空指针。第148行是一个},这是onClickNo的结束。根据堆栈跟踪,第148行必须位于
onActivityResult()的
onActivityResult()
中。imageView为空,因为
onActivityResult
发生在
onCreate
之前
Glide.with(this)
                .load(uri)
                .dontAnimate()
                .placeholder(Drawable)
                .into(ImageView);