在Activity被销毁后,ImageView上的图像丢失了 [英] Image on ImageView lost after Activity is destroyed

查看:147
本文介绍了在Activity被销毁后,ImageView上的图像丢失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用,我可以让用户选择要在其个人资料中显示的图片。我可以在imageview上浏览和设置他们选择的图像。但是一旦活动被破坏,图像就会丢失。我试图实现onSaveInstanceState,但它仍然是相同的。我想知道我是否正确使用它。我希望你能帮助像我这样的新手。提前致谢。这是我正在使用的代码:

  public class AccountFragment extends Fragment实现OnClickListener {
private LoginDataBaseAdapter loginDataBaseAdapter;
位图图片;
位图位图;
字符串picture_location;
TextView textTargetUri;
ImageView targetImage;

@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){


查看rootView = inflater.inflate (R.layout.fragment_account,container,false);


textTargetUri =(TextView)rootView.findViewById(R.id.targeturi);

targetImage =(ImageView)rootView.findViewById(R.id.profpic);

targetImage.setOnClickListener(new ImageView.OnClickListener(){
@Override
public void onClick(View arg0){
Intent intent = new Intent(Intent.ACTION_PICK) ,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,0);
}});

if(savedInstanceState!= null){
//如果有一个包,请使用保存的图像资源(如果有的话)
image = savedInstanceState.getParcelable( BitmapImage的);
targetImage.setImageBitmap(image);
textTargetUri.setText(savedInstanceState.getString(path_to_picture));
}



返回rootView;



}

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelable(BitmapImage,bitmap);
savedInstanceState.putString(path_to_picture,picture_location);
}


@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data );

if(resultCode == Activity.RESULT_OK){
Uri targetUri = data.getData();
picture_location = targetUri.toString();
textTargetUri.setText(targetUri.toString());
位图位图;
try {
bitmap = BitmapFactory.decodeStream(getActivity()。getContentResolver()。openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
}

@Override
public void onClick(查看arg0){
// TODO自动生成的方法stub

}}

顺便说一下,你可能已经注意到了而不是使用在oncreate之后的onRestoreInstanceState,我尝试使用不同的方法。我从另一个问题中找到了答案,你也可以在oncreate中实现它。我使用它,因为每当我声明函数onRestoreInstanceState时,我都会被要求删除@Override注释。

  @Override 
protected void onRestoreInstanceState(Bundle savedInstanceState){
image = savedInstanceState.getParcelable(BitmapImage);
targetImage.setImageBitmap(image);
textTargetUri.setText(savedInstanceState.getString(path_to_picture));
}


解决方案

使用onSaveInstanceState和onCreate / onRestoreInstanceState用于短期活动状态保存 - 但不能用于持久存储应用程序的数据。



您可以阅读onSaveInstanceState 此处



您可以阅读有关持久存储的信息此处



codeMagic建议您使用SharedPrefs(请参阅持久存储链接)进行长期持久存储。如果你想这样做,我建议你在onActivityResult方法中保存图像URI(链接有一个很好的例子),然后调用一个方法来读取SharedPref并加载你可以调用的图像onCreate以及onActivityResult。



您可能还希望将自己的映像/位图副本存储在应用程序自己的内部存储中(请参阅持久存储链接)。 / p>

I am trying to make an app where I can let a user select a picture to display on their profile. I am able to browse and set their selected image on imageview. But the image is lost once the the activity is destroyed. I tried to implement onSaveInstanceState but still it's the same. I'm wondering if I am using it correctly. I hope you can help a newbie like me. Thanks in advance. Here's the code that I'm using:

public class AccountFragment extends Fragment implements OnClickListener {
private LoginDataBaseAdapter loginDataBaseAdapter;
Bitmap image;
Bitmap bitmap;
String picture_location;
TextView textTargetUri;
ImageView targetImage;

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


        View rootView = inflater.inflate(R.layout.fragment_account, container, false);


            textTargetUri = (TextView) rootView.findViewById(R.id.targeturi);

            targetImage=(ImageView) rootView.findViewById(R.id.profpic);

            targetImage.setOnClickListener(new ImageView.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Intent   intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }});

            if (savedInstanceState != null) {
                //if there is a bundle, use the saved image resource (if one is there)
                image = savedInstanceState.getParcelable("BitmapImage");
                targetImage.setImageBitmap(image);
                textTargetUri.setText(savedInstanceState.getString("path_to_picture"));
            } 



        return rootView;



    }

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelable("BitmapImage", bitmap);
        savedInstanceState.putString("path_to_picture", picture_location);
    }


    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK){
            Uri targetUri = data.getData();
            picture_location = targetUri.toString();
            textTargetUri.setText(targetUri.toString());
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
                targetImage.setImageBitmap(bitmap);
            } 
            catch (FileNotFoundException e){
                e.printStackTrace();
            }
        }   
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }}

By the way, you may have noticed that instead of using the onRestoreInstanceState after oncreate, I tried to use the different approach. I found an answer from another question that you can also implement it inside the oncreate. I used it since whenever I declare the function onRestoreInstanceState I am being asked to remove the @Override annotation.

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState){       
            image = savedInstanceState.getParcelable("BitmapImage");
            targetImage.setImageBitmap(image);
            textTargetUri.setText(savedInstanceState.getString("path_to_picture"));
    }

解决方案

Using onSaveInstanceState and onCreate/onRestoreInstanceState is for short term activity state preservation - but not to be used for persistent storage of the application's data.

You can read about onSaveInstanceState here

You can read about persistent storage here

codeMagic suggested using SharedPrefs (see persistent storage link) for your long-term persistent storage. If you wanted to do this, I would suggest saving the image URI (the link has a good example of how to do so) in you onActivityResult method, and then call a method to read the SharedPref and load the image that you can call from onCreate as well as from onActivityResult.

You may also want to store your own copy of the image/bitmap in your application's own internal storage (see persistent storage link).

这篇关于在Activity被销毁后,ImageView上的图像丢失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆