当使用相同名称上传图像时,android图像显示旧图像而不是新图像

时间:2016-06-07 04:23:31

标签: java php android

ProfileActivity.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_profile, container,
            false);
    nama = (TextView) rootView.findViewById(R.id.nama);
    tanggal = (TextView) rootView.findViewById(R.id.birth);
    telepon = (TextView) rootView.findViewById(R.id.telepon);
    status = (TextView) rootView.findViewById(R.id.status);
    email = (TextView) rootView.findViewById(R.id.email);
    img = (ImageView) rootView.findViewById(R.id.image);

    edit = (Button) rootView.findViewById(R.id.edit);

    session = new SessionManager(getActivity());
     int loader = R.drawable.ic_launcher;
    session.checkLogin();
    HashMap<String, String> user = session.getUserDetails();
    String nama1 = user.get(SessionManager.KEY_username);
    String tanggal1 = user.get(SessionManager.key_birthday);
    String image1 = user.get(SessionManager.key_image);
    String status1 = user.get(SessionManager.key_status);
    String telepon1 = user.get(SessionManager.key_telephone);
    String email1 = user.get(SessionManager.key_email);
    nama.setText("Nama: " + nama1);
    tanggal.setText("Tanggal Lahir : " + tanggal1);
    telepon.setText("No Telepon : " + telepon1);

    if (status1.equals("SA")) {
        status.setText("Status : Super Admin");
    } else if (status1.equals("A")) {
        status.setText("Status : Admin");
    } else {
        status.setText("Status : Sales");
    }
    email.setText("Email : " + email1);

    edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Fragment fragment = new EditProfileActivity();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();
            getActivity().setTitle("Edit Profile");
        }
    });
    String image_url = "url_name"
            + image1;

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getActivity());

    imgLoader.DisplayImage(image_url, loader, img);

    return rootView;
}

EditProfileActivity.java

session = new SessionManager(getActivity());
        session.checkLogin();
        HashMap<String, String> user = session.getUserDetails();
        telp1 = user.get(SessionManager.key_telephone);
        email1 = user.get(SessionManager.key_email);
        id = user.get(SessionManager.KEY_ID);
        nama = user.get(SessionManager.KEY_username);
        image1 = user.get(SessionManager.key_image);
        tanggal = user.get(SessionManager.key_birthday);
        status = user.get(SessionManager.key_status);
        password1 = user.get(SessionManager.key_pass);
        oldpass1 = oldpass.getText().toString();
        newpass1 = newpass.getText().toString();
        retypepass1 = retypepass.getText().toString();
        int loader = R.drawable.ic_launcher;
        gmbr.setText(image1);
        telp.setText(telp1);
        email.setText(email1);
        oldpass.setText(password1);
        newpass.setText(password1);
        String image_url = "url_name"
                + image1;

        // ImageLoader class instance
        ImageLoader imgLoader = new ImageLoader(getActivity());

        imgLoader.DisplayImage(image_url, loader, img);

        changepic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                galleryIntent();
            }
        });

        editprof.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // if (!oldpass.equals(password1) ||
                // !retypepass.equals(newpass)) {
                // Toast.makeText(getActivity(), "Old Password Salah",
                // Toast.LENGTH_SHORT).show();
                // } else {
                new editprofile().execute();
                // }
            }
        });

        return rootView;
    }

    protected void galleryIntent() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_FILE);
    }

    @SuppressWarnings("static-access")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_FILE && resultCode == getActivity().RESULT_OK
                && data != null) {
            Uri Selectedimage = data.getData();
            img.setImageURI(Selectedimage);
        }
    }

    class editprofile extends AsyncTask<Void, Integer, String> {

        @Override
        protected void onPreExecute() {
            // setting progress bar to zero
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(Void... params) {
            Bitmap images = ((BitmapDrawable) img.getDrawable()).getBitmap();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            images.compress(Bitmap.CompressFormat.JPEG, 100,
                    byteArrayOutputStream);
            String EncodedImage = Base64.encodeToString(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
            String email2 = email.getText().toString();
            String telepon2 = telp.getText().toString();
            String newpass2 = newpass.getText().toString();
            String img2 = gmbr.getText().toString();

            ArrayList<NameValuePair> datatosend = new ArrayList<NameValuePair>();
            datatosend.add(new BasicNameValuePair("image", EncodedImage));
            datatosend.add(new BasicNameValuePair("imagename", img2));
            datatosend.add(new BasicNameValuePair("email", email2));
            datatosend.add(new BasicNameValuePair("telepon", telepon2));
            datatosend.add(new BasicNameValuePair("newpass", newpass2));
            datatosend.add(new BasicNameValuePair("id", id));

            HttpParams httpParams = gethttprequestparam();
            HttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpPost httpPost = new HttpPost(
                    EditProfileActivity.url_create_product);

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(datatosend));
                httpClient.execute(httpPost);

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            Toast.makeText(getActivity(), "edit profile berhasil",
                    Toast.LENGTH_SHORT).show();
            session.logoutUser();
        }

    }

    public void onBackPressed() {
        FragmentManager fm = getActivity().getFragmentManager();
        fm.popBackStack();
    }

    public HttpParams gethttprequestparam() {
        // TODO Auto-generated method stub
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 1000 * 30);
        HttpConnectionParams.setSoTimeout(httpParams, 1000 * 30);
        return httpParams;
    }

EditProfile.php

<?php 
$response = array();


include('Connect.php');
$image= $_POST['image'];
$image1= $_POST['imagename'];
$email= $_POST['email'];
$telepon= $_POST['telepon'];
$newpass= $_POST['newpass'];
$id= $_POST['id'];


$decodedimage = base64_decode("$image");
$image = 
file_put_contents("foldername/".$image1, $decodedimage);
$query = mysqli_query($con, "update user set image = '$image1' where iduser = '$id'");
$query1 = mysqli_query($con, "update user set email = '$email' where iduser = '$id'");
$query2 = mysqli_query($con, "update user set phone = '$telepon' where iduser = '$id'");
$query3 = mysqli_query($con, "update user set password = '$newpass' where iduser = '$id'");

echo json_encode($response);
?>

主要问题是当我使用相同的图像名称编辑配置文件时,数据库中的表用户被更新并且图像上载了具有相同名称的新图像但是当我使用imageloader在android中显示图像时,图像显示为旧的而不是新的。我该怎么办呢?

注意事项:当图像名称不同时,图像会被替换为新图像(例如:旧图像是a.JPG,新图像是b.JPG)但是赢了如果图像名称仍然相同,则替换为新的。

0 个答案:

没有答案