ImageView没有更新onActivityResult的更改

时间:2016-12-15 17:41:37

标签: java android imageview picasso ui-thread

我尝试过使用Picasso以及我在堆栈溢出时可能找到的所有方法。我用相机拍摄图像并获得onActivity结果的值。只有在我退出并重新启动应用程序后,imageView才会更新。我想在我眼前改变imageview。这些代码似乎都没有工作。

我试过这个

    public static void main(String[] args) {
        String newDaysAdded = "05";
        String month = "04"; 
        String year = "2014";
        String newDateString = (newDaysAdded+"/"+month+"/"+year);

        SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date newDate = SDF.parse(newDateString); //Takes string and convert it to date

            //If i understand the question you dont just want to print it as a date...
            System.out.println(newDate);   //Sat Apr 05 00:00:00 CAT 2014

            //You want to print it as a string inf the format you specified in the SDT
            System.out.println(SDF.format(newDate)); //Print  as in05/04/2014
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

在点击确定后关闭相机视图时没有任何反应。

我也试过这个:

getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    profile.setImageBitmap(thumbnail);
                }
            });

但是UI没有变化,图像保持不变。

我尝试过异步任务,Picasso也没有。

Idk如何做到这一点,已经有一段时间了,我非常沮丧

以下是两种技术的代码:

   Thread thread = new Thread()
            {
                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() //run on ui thread
                    {
                        public void run()
                        {
                            profile.setImageBitmap(thumbnail);

                        }
                    });
                }
            };
            thread.start();

1 个答案:

答案 0 :(得分:0)

ImageView未更新的原因是:1)Android将加载缓存中的先前图像,只要您将图像保存到相同的目的地2)onActivityResult是在onResume之前调用。这意味着调用ImageView时,您加载到onResume中的图像会丢失。

第一个问题可以由Glide的{​​{1}}处理,而第二个问题可以由signature处理。

更改图像时创建一个onSavedInstanceState()。每次更改都应有所不同,例如当前时间。然后在加载图像时应用Glide signature

signature

然后将新的public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && data != null) { final Bitmap bitmap = (Bitmap) data.getExtras().get("data"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes); final File destination = new File( Environment.getExternalStorageDirectory() .getAbsolutePath(), "temp.jpg"); FileOutputStream fo; try { fo = new FileOutputStream(destination); fo.write(bytes.toByteArray()); fo.flush(); } catch (IOException e) { e.printStackTrace(); } Uri uri = Uri.fromFile(destination) //Create your signature SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss", Locale.getDefault()); String timeStamp = dateFormat.format(new Date()); newImageSignature = new RequestOptions() .signature(new ObjectKey(timeStamp)); //Load your image Glide.with(yourActivity.this) // Or Fragment .load(bitmap) // Or Uri .apply(newImageSignature) .into(imageView); // You can save the uri and signature to Firebase Database, // and your image to Storage here 保存在signature

onSaveInstanceState()

现在您可以在@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (newImageSignature != null) { outState.putString("IMAGE_SIGNATURE”, newImageSignature.toString()); outState.putString("IMAGE_URI”, uri.toString()); } } 中检索signature

onCreate()

注意:我只是写了这个,还没有测试。希望能给