【问题标题】:Android Profile ImageAndroid 个人资料图片
【发布时间】:2017-01-06 06:20:16
【问题描述】:

我制作了一个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并选择一张图片,它会在几秒钟内当场更新,应该不需要再次打开应用程序,尽快建议我

【问题讨论】:

  • 清除毕加索的缓存,然后再试一次

标签: android android-imageview android-bitmap


【解决方案1】:

试试这个:

 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);

它会自动刷新文件..

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      清除毕加索的缓存。如果不清除缓存,它会从缓存中加载图像。要么 试试这个代码:

      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);
      

      【讨论】:

        【解决方案4】:

        在您的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);
               }
        

        【讨论】:

          猜你喜欢
          • 2013-11-20
          • 2015-04-14
          • 2017-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-08
          相关资源
          最近更新 更多