本文介绍了Bitmap image =((bitmapdrawable)imageview.getdrawable())。getbitmap(); 。给出空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个通过android上传图像到Web服务器的项目..但我在 BitmapDrawable 上得到了一个Error null点异常。请帮助我...



我尝试过的事情:



i have created a projectfor uploading an image to web server through android.. but I got an Error null point exception on BitmapDrawable. plz help me...

What I have tried:

selectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //call the function to select image from album
            selectImage();
        }
    });

    //when uploadImage button is pressed
    uploadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            //get image in bitmap format
            Bitmap image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            //execute the async task and upload the image to server
            new Upload(image,"IMG_"+timestamp).execute();
        }
    });

}

//function to select a image
private void selectImage(){
    //open album to select image
    Intent gallaryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(gallaryIntent, RESULT_SELECT_IMAGE);
}

/*
* This function is called when we pick some image from the album
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_SELECT_IMAGE && resultCode == RESULT_OK && data != null){
        //set the selected image to image variable
        Uri image = data.getData();
        imageView.setImageURI(image);

        //get the current timeStamp and strore that in the time Variable
        Long tsLong = System.currentTimeMillis() / 1000;
        timestamp = tsLong.toString();

        Toast.makeText(getApplicationContext(),timestamp,Toast.LENGTH_SHORT).show();
    }
}

private String hashMapToUrl(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}


//async task to upload image
private class Upload extends AsyncTask<Void,Void,String>{
    private Bitmap image;
    private String name;

    public Upload(Bitmap image,String name){
        this.image = image;
        this.name = name;
    }

    @Override
    protected String doInBackground(Void... params) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //compress the image to jpg format
        image.compress(Bitmap.CompressFormat.JPEG,0,byteArrayOutputStream);
        /*
        * encode image to base64 so that it can be picked by saveImage.php file
        * */
        String encodeImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

        //generate hashMap to store encodedImage and the name
        HashMap<String,String> detail = new HashMap<>();
        detail.put("name", name);
        detail.put("image", encodeImage);

        try{
            //convert this HashMap to encodedUrl to send to php file
            String dataToSend = hashMapToUrl(detail);
            //make a Http request and send data to saveImage.php file
            String response = Request.post(SERVER,dataToSend);

            //return the response
            return response;

        }catch (Exception e){
            e.printStackTrace();
            Log.e(TAG,"ERROR  "+e);
            return null;
        }
    }



    @Override
    protected void onPostExecute(String s) {
        //show image uploaded
        Toast.makeText(getApplicationContext(),"Image Uploaded",Toast.LENGTH_SHORT).show();
    }
}

推荐答案

imageView.setImageURI(null);
imageView.setImageURI(image);




活动结果中的




应该这样做。



in the activityresult.

that should do it.


这篇关于Bitmap image =((bitmapdrawable)imageview.getdrawable())。getbitmap(); 。给出空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:30