2016-01-28 61 views
0

我有一个应用程序,我允许用户从Galery中选取图片,并将其用作PROFILE PICTURE,选择图片并在我的应用程序的“ImageView”中设置。退出应用程序后保存ImageView状态(onSaveInstanceState)

问题是,当应用程序关闭时,您的活动被更改为图片desapear,或返回默认图片再次,我想要保存此图片状态为何时回到活动或关闭并重新打开应用程序照片继续存在,不需要重新设置。 我是新开发人员,如果你能帮我看下我的代码,并给出所需的修改,并给我现成的代码,我将非常感激,因为我花了好几天的时间做这件事,但我仍然无法做到。我需要一个现成的代码,因为我是开发新手,如果你尝试解释一些我不会理解的东西。

这里是我的代码,我挑图片:

public class MainActivity extends Activity { 
private static int RESULT_LOAD_IMG = 1; 
String imgDecodableString; 

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

public void loadImagefromGallery(View view) { 
    // Create intent to Open Image applications like Gallery, Google Photos 
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    // Start the Intent 
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 
      ImageView imgView = (ImageView) findViewById(R.id.imgView); 
      // Set the Image in ImageView after decoding the String 
      imgView.setImageBitmap(BitmapFactory 
        .decodeFile(imgDecodableString)); 

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

} 

}

回答