【问题标题】:How do I make that different button, different imageview will be affected如何制作不同的按钮,不同的图像视图会受到影响
【发布时间】:2019-07-22 11:01:07
【问题描述】:

我对这个编码很陌生,我希望你们中的任何人都可以帮助我解决这个问题。基本上我有三个具有不同图像视图的不同按钮,我想将它们上传到我的 MySQL。但是,当我在不同时间按下所有三个按钮时,它只会影响图像视图中的 1 个。如果您能提供帮助,不胜感激。

    public static final int resultloadimage = 1;
    private static final int RESULT_OK = -1;

    Button btnupadloadnric, btnuploaddl, btnuploadvl,updateButton;
    ImageView ivnric,ivdl,ivvl;
    EditText fullanme, telephone;
    String encodedimage;
    ConnectionClass connectionClass;
    String filename = null;
    byte[] image = null;
    private byte[] byteArray;


    public Profile() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_profile, container, false);

        btnupadloadnric = v.findViewById(R.id.btnAdminUploadNRICFragment);
        btnuploaddl = v.findViewById(R.id.btnAdminUploadDLFragment);
        btnuploadvl = v.findViewById(R.id.btnAdminUploadVCLFragment);
        ivdl = v.findViewById(R.id.ivadminprofiledl);
        ivnric = v.findViewById(R.id.ivadminprofilenric);
        ivvl = v.findViewById(R.id.ivadminprofilevl);
        fullanme = v.findViewById(R.id.etAdminFullNameProfileFragment);
        telephone = v.findViewById(R.id.etAdminPhoneNumberProfileFragment);
        updateButton = v.findViewById(R.id.btnAdminUpdateProfileFragment);
        connectionClass = new ConnectionClass();


        btnuploadvl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, resultloadimage);
            }
        });
        btnuploaddl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, resultloadimage);
            }
        });
        btnupadloadnric.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, resultloadimage);

            }
        });
        updateButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                UploadImage uploadImage = new UploadImage();
                uploadImage.execute("");
            }


        });

    return v;
    }
    @Override
    public void onActivityResult(int requestCode, int resultcode, Intent data)
    {
        super.onActivityResult(requestCode,resultcode,data);
        if (requestCode == resultloadimage && resultcode == RESULT_OK && null !=data){
            Bitmap originbitmap = null;
            Uri selectedImage = data.getData();
            InputStream imagestream;
            try {
                imagestream = getActivity().getContentResolver().openInputStream(selectedImage);
                originbitmap = BitmapFactory.decodeStream(imagestream);


            }catch (FileNotFoundException e)
            {
                Toast.makeText(getActivity(),"Done",Toast.LENGTH_LONG).show();
            }
            if (originbitmap!=null){
                this.ivdl.setImageBitmap(originbitmap);
                Log.w("Image in", "Done");
                try {
                    Bitmap image = ((BitmapDrawable)ivdl.getDrawable()).getBitmap();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 90 , byteArrayOutputStream);
                    byteArray = byteArrayOutputStream.toByteArray();
                    encodedimage = Base64.encodeToString(byteArray,Base64.DEFAULT);
                    UploadImage uploadImage = new UploadImage();
                    uploadImage.execute("");

                }catch (Exception e){
                    Log.w("asd", "Exception");
                }
                Toast.makeText(getActivity(),"Done",Toast.LENGTH_LONG).show();
            }

        }
        else{
System.out.println("Error Occured");
        }

    }


    public class UploadImage extends AsyncTask<String,String,String>
    {
        @Override
        protected String doInBackground(String... strings) {
            try {
                Connection con = connectionClass.CONN();
                if (con==null) {
                    Toast.makeText(getActivity(),"Check Internet", Toast.LENGTH_LONG).show();
                }else{
                    String command = "Insert into driverprofile (DrivingL, Username, Password) values('" + encodedimage + "', 'Admin1', '12345')";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(command);
                    if (rs.next()){}
                }

            } catch (Exception ex) {
                ex.getMessage();
            }

            return null;
        }
    }
}

【问题讨论】:

    标签: java android mysql


    【解决方案1】:

    我认为您应该在每次按钮点击时更改每个图库意图的请求代码。

    btnuploadvl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, "request code for btnuploadvl");
            }
        });
        btnuploaddl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, "request code for btnuploaddl");
            }
        });
        btnupadloadnric.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, "request code for btnupadloadnric");
    
            }
        });
    

    然后检查请求代码,检测onActivityResult上点击了哪个按钮。

    【讨论】:

    • 嘿 faranjit,感谢您回复我。我确实试过了,但还是不行。
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多