简体   繁体   English

ImageView在onActivityResult中的更改时未更新

[英]ImageView not updating on change in onActivityResult

I have tried using Picasso and every method I could possibly find here on stackoverflow. 我已经尝试过使用Picasso和在stackoverflow上可以找到的每种方法。 I'm taking an image with camera and getting the value onActivity result. 我正在用相机拍摄图像,并获得onActivity结果的值。 The imageView gets updated only after I quit and restart the application. 仅在退出并重新启动应用程序后,imageView才会更新。 I want imageview to change before my eyes. 我希望在我眼前改变imageview。 None of the code seems to be working. 这些代码似乎都不起作用。

I tried this 我试过了

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

nothing happens when camera view closes after hitting ok. 单击确定后关闭相机视图时,没有任何反应。

I also tried this: 我也试过这个:

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

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

but UI doesn't change, image remains the same. 但UI不变,图像保持不变。

I tried Async tasks, Picasso doesn't work either. 我尝试了异步任务,毕加索也不起作用。

Idk how to do this, it's been a while and I'm super frustrated Idk怎么做,已经有一段时间了,我非常沮丧

Here is the code with both techniques: 这是两种技术的代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1 && data != null) {
            final Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.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.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            uploadProfilePicToS3(destination.getAbsolutePath());

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

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

            //not working either!
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    profile.setImageBitmap(thumbnail);
                }
            });

The reason your ImageView is not updated is because 1) Android loads the previous image that is in the cache, as long as you're saving the image to the same destination 2) onActivityResult is called before onResume . ImageView未更新的原因是:1)Android将加载缓存中的先前图像,只要您将图像保存到相同的目的地2)在onResume之前调用onActivityResult This means that the image you loaded into the ImageView is lost when onResume is called. 这意味着在调用onResume时,您加载到ImageView中的ImageView会丢失。

The first problem is nicely handled by Glide 's signature , and the second problem can be handled with onSavedInstanceState() . 第一个问题可以通过Glidesignature很好地解决,第二个问题可以使用onSavedInstanceState()处理。

Create a Glide signature when you change the image. 更改图像时创建一个Glide signature This should be different for each change eg current time. 每次更改(例如,当前时间)应该不同。 Then apply the signature as you load the image. 然后在加载图像时应用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

Then save the new signature in onSaveInstanceState() 然后将新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());
    }
}

Now you can retrieve the signature in onCreate() 现在您可以在onCreate()检索signature

Uri ImageUri;
ImageView imageView;
RequestOptions imageSignature;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Retrieve your saved parameters. Or get them from a database.
    if (savedInstanceState != null){
        imageSignature = new RequestOptions()
            .signature(new 
             ObjectKey(savedInstanceState.getString("IMAGE_SIGNATURE"))); 
        imageUri = Uri.parse(savedInstanceState.getString("IMAGE_URI"));

    // Instantiate your imageview

    // Load image
    if(imageUri != null && imageSignature != null){
        Glide.with(yourActivity.this) // Or Fragment
        .load(bitmap) // Or Uri
        .apply(newImageSignature)
        .into(imageView);
    }
}

NB: I just wrote this, not tested yet. 注意:我只是写了这个,还没有测试。 Hope it gives 希望它能给

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM