在Activity被销毁后,ImageView上的图像丢失了

时间:2014-08-29 14:22:41

标签: android android-fragments imageview onrestoreinstancestate

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

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

    }}

顺便说一句,你可能已经注意到,在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"));
    }

4 个答案:

答案 0 :(得分:1)

如果您没有完成活动,可以使用onSavedInstance()存储picture_location值并将其绑定回来onCreate(SavedInst) / onRestore() picture_location价值。

答案 1 :(得分:1)

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

您可以阅读onSaveInstanceState here

您可以阅读有关持久存储空间here

的信息

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

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

答案 2 :(得分:0)

如果是位图实例状态不是建议的方法来保留有关所选图像的信息。

您可以在此处找到解释:Handling configuration Changes

我在这里写了很多关于它的博客:Retain selected Image during Screen Rotation

下面我粘贴我所说明的解决方案的实现:

1 - 创建片段并将其配置为保留在内存中

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class ImageRetainingFragment extends Fragment {
    private Bitmap selectedImage;

    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // retain this fragment        
        setRetainInstance(true);
}

public void setImage(Bitmap selectedImage) {
    this.selectedImage = selectedImage;
}

public Bitmap getImage() {
    return this.selectedImage;
}
}

2 - 在您的活动

中使用它
private static final String FRAGMENT_NAME = "imageFragment";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ....

    initializeImageRetainingFragment();
    tryLoadImage();
}


private void initializeImageRetainingFragment() {
    // find the retained fragment on activity restarts    
    FragmentManager fragmentManager = getSupportFragmentManager();
    this.imageRetainingFragment = (ImageRetainingFragment)  fragmentManager.findFragmentByTag(FRAGMENT_NAME);
    // create the fragment and bitmap the first time   
    if (this.imageRetainingFragment == null) {
         this.imageRetainingFragment = new ImageRetainingFragment();
         fragmentManager.beginTransaction()
         // Add a fragment to the activity state. 
         .add(this.imageRetainingFragment, FRAGMENT_NAME)
         .commit();
   }
}
private void tryLoadImage() {
   if (this.imageRetainingFragment == null) {
       return;
   }

   Bitmap selectedImage = this.imageRetainingFragment.getImage();
      if (selectedImage == null) {
        return;
      }

      ImageView selectedImageView = (ImageView)findViewById(R.id.selectedImage);
      selectedImageView.setImageBitmap(selectedImage);
}

答案 3 :(得分:0)

1)// manifest.xml

2)//公共类MainActivity扩展AppCompatActivity实现LocationListener{...

SharedPreferences.Editor editor;

3)//protected void onCreate(Bundle savedInstanceState){ ...

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
            PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
    }

4)//共享偏好 SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", getApplicationContext().MODE_PRIVATE); editor = pref.edit();

5)//设置图片路径 if (pref.getString("mydraw", null) != null) {

        img6.setImageURI(Uri.parse(pref.getString("mydraw", null)));

    } else {
        //set default image
        img6.setImageResource(R.drawable.poseidon);
    }

6)//protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {...

如果(请求代码 == 100){ if (resultCode == RESULT_OK) {

            img6.setImageURI(data.getData());

            //save URI as string
            editor.putString("mydraw", data.getData().toString());
            editor.commit(); // commit changes

        }
    }