如何在doInbackground中获取位图的值

时间:2019-02-15 10:41:08

标签: android

我想获取Bitmap图片的值以放入BackgroundTask,以便在尝试获取位图时可以将其转换为base64以使其成为字符串并将其传递给php “分配给可变图像无效”

抱歉我的语法,我真的很想学习android,希望您能理解。

public void addInfo(View v)
{

    spass= pass.getText().toString();
    scpass = cpass.getText().toString();

    if(spass.equals(scpass))
    {
        BackgroundTask backgroundTask = new BackgroundTask();
        backgroundTask.execute(sfn,sln,sun,spass,spid,sem,scn);


    }
    else
    {
        Toast.makeText(getApplicationContext(), "Password is not match", Toast.LENGTH_SHORT).show();
        pass.setText("");
        cpass.setText("");
    }
}

我想在后台任务中传递位图

class BackgroundTask extends AsyncTask<String, Void, String>
{
    String add_info_url;
    Bitmap image;

    @Override

    protected void onPreExecute() {
        // TODO Auto-generated method stub
        add_info_url = "https://ralph-rojas1996.000webhostapp.com/android/register.php";

    }





    @Override
    protected String doInBackground(String... args) {
        String fname,lname,uname,pass,pid,email,cn;
        // TODO Auto-generated method stub
        this.image = image;
        fname = args[0];
        lname = args[1];
        uname = args[2];
        pass = args[3];
        pid = args[4];
        email = args[5];
        cn = args[6];
        ByteArrayOutputStream byteArrayOututStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOututStream);
        String encodedImage = Base64.encodeToString(byteArrayOututStream.toByteArray(),Base64.DEFAULT);
        try {

            StringBuilder JSON_DATA = new StringBuilder();
            URL url = new URL(add_info_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data_string = URLEncoder.encode("fname","UTF-8")+"="+URLEncoder.encode(fname,"UTF-8")+"&"+
                    URLEncoder.encode("lname","UTF-8")+"="+URLEncoder.encode(lname,"UTF-8")+"&"+
                    URLEncoder.encode("uname","UTF-8")+"="+URLEncoder.encode(uname,"UTF-8")+"&"+
                    URLEncoder.encode("pass","UTF-8")+"="+URLEncoder.encode(pass,"UTF-8")+"&"+
                    URLEncoder.encode("pid","UTF-8")+"="+URLEncoder.encode(pid,"UTF-8")+"&"+
                    URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                    URLEncoder.encode("cn","UTF-8")+"="+URLEncoder.encode(cn,"UTF-8")+"&"+
                    URLEncoder.encode("image","UTF-8")+"="+URLEncoder.encode(encodedImage,"UTF-8");
                    bufferedWriter.write(data_string);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();
                    InputStream inputStream = httpURLConnection.getInputStream();
                     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        while ((JSON_STRING = reader.readLine())!=null) {
                            JSON_DATA.append(JSON_STRING).append("\n");
                        }
                        String x = JSON_DATA.toString().trim();
                     if(x.equals("1"))
                        {

                            return y;
                        }
                        else if(x.equals("2"))
                        {
                        return z;
                        }
                        else
                        {

                            return a;
                        }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        boom = result;
        onLoginResponse(boom);
    }
}
public void upi(View v)
{
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
}
private void onLoginResponse(String json){
   if(json.equals(a))
   {
       finish();
   }
   else if (json.equals(y))
   {
       un.setText("");
   }
   else if (json.equals(z))
   {
       pid.setText("");
   }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data !=null)
    {
        Uri selectedImage = data.getData();
        itu.setImageURI(selectedImage);         
    }
}
public void home (View v)
{
    Intent i = new Intent (this,MainActivity.class);
    startActivity(i);
}
}

我想获取位图,将其转换为 base64

0 个答案:

没有答案