Android个人资料图片

时间:2017-01-06 06:20:17

标签: android android-imageview android-bitmap

我制作了一个Activity,其中用户从图库中选择图片并将此图片设置为圆形ImageView, 这是我的代码部分:

public void loadImagefromGallery(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);      //camera intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
            && null != data) {
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        cursor.close();
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
        //  imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.ivprofileimg);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] byteArrayImage = baos.toByteArray();
        encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
        new saveprofileimage().execute(custid, encodedImage);

    } else {
        Toast.makeText(this, "You haven't picked Image",
                Toast.LENGTH_LONG).show();
    }

} catch (Exception e) {
    Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
            .show();
}

}


class saveprofileimage extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
    pdilog = new ProgressDialog(MyProfile.this);
    //     pdilog.setMessage("Please Wait....");
    pdilog.show();
    super.onPreExecute();
}


@Override
protected Void doInBackground(String... params) {
    try {
        response = SaveProfileImage.getJSONfromURL(params[0] + " ", params[1]);
        JSONObject _jobject = new JSONObject(response);
        serverresult = _jobject.getString("Result");
        profileimagee = _jobject.getString("ProfileImage");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}


@Override
protected void onPostExecute(Void result) {
    pdilog.dismiss();
    if (serverresult.equals("Success")) {
        Picasso.with(MyProfile.this)
                .load(profileimagee)
                .into(imgView);
        new customerprofiledetails().execute(custid);
    } else {
        Toast.makeText(MyProfile.this, "Image couldn't be saved", Toast.LENGTH_LONG).show();
    }

    super.onPostExecute(result);

}
}

问题是,当ImageView中的图像已经存在并且之后我尝试更新图像时,图像不会更新,但是当用户关闭并重新打开应用程序时,更新的图像是在ImageView, 我想要的是每当我点击ImageView并选择一个图像时,它会在几秒钟内就地更新,不应该再次打开应用程序,建议我尽快

4 个答案:

答案 0 :(得分:2)

试试这个:

 public class PicassoTools {

    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}

使用:

来调用它
PicassoTools.clearCache(Picasso.with(context));

您也可以尝试:

 Picasso.with(MyProfile.this)
        .load(profileimagee)
        .memoryPolicy(MemoryPolicy.NO_CACHE )
        .networkPolicy(NetworkPolicy.NO_CACHE)
        .into(imgView);

它会自动刷新文件..

答案 1 :(得分:2)

这是因为本地缓存驻留在android内存中。尝试清理缓存。

答案 2 :(得分:2)

清除毕加索的缓存。如果不清除高速缓存,则会从高速缓存中加载映像。要么 试试这段代码:

Glide.with(getApplicationContext()).load("image_url")
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.listener(new RequestListener<String, GlideDrawable>() {
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        progressBar.setVisibility(View.GONE);
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        progressBar.setVisibility(View.GONE);
        return false;
    }
})

.into(profileImage);

答案 3 :(得分:1)

onPostExecute内,只需修改如下

    @Override
    protected void onPostExecute(Void result) {
        pdilog.dismiss();
        if (serverresult.equals("Success")) {
            Picasso.with(MyProfile.this)
                    .load(profileimagee)
                    .into(imgView);
       finish();
       startActivity(getApplicationContext(),YourActivityName.class);
       }