Android 碎片翻转并丢失图像
我已完全重新编写了问题,以缩小范围。 我有两个像卡片一样翻转的碎片(左,右)。当前面的片段消失时,它会翻转显示后面。再次单击按钮后,它将再次翻转到前面,但前面片段上的ImageView已消失 我尝试了不同的方法来保存所拾取图像的数据Android 碎片翻转并丢失图像,android,android-fragments,Android,Android Fragments,我已完全重新编写了问题,以缩小范围。 我有两个像卡片一样翻转的碎片(左,右)。当前面的片段消失时,它会翻转显示后面。再次单击按钮后,它将再次翻转到前面,但前面片段上的ImageView已消失 我尝试了不同的方法来保存所拾取图像的数据 在SaveInstanceState上保存片段 这给了我一个空指针,所以我想一旦被创建,我需要一些更恒定的东西 因此,现在我将图像保存到SDCard,一旦拾取 我认为这样做是可行的,只要检查路径并抓住它,如果它翻到前面或者活动被重新创建 这里有一些代码 onCrea
//flip card
final Button cardBackButton = (Button) findViewById(R.id.cardBackButton);
cardBackButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
flipCard();
});
动画卡方法:
private void flipCard()
{
Log.d(tag2, "Log after flipCard:" + mShowingBack);
if(mShowingBack)
{
//Flip to front
flipFront();
return;
}
// Flip to back
flipBack();
}
我从他们的照片库中将图像设置为ActivityResult
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Uri photoUri = intent.getData();
if (photoUri != null) {
try {
ImageView setImage = (ImageView)findViewById(R.id.imageView);
frontImage = MediaStore.Images.Media.getBitmap(this
.getContentResolver(), photoUri);
imageSet = true;
//save image to SD
if(imageSet == true){
Log.d(tag, "Inside Image Set if Statement");
String path = getExternalCacheDir() + "Postcards.png";
if(path != null && path != ""){
Log.d(tag, "Path is:" + path);
File file = new File(path);
newPath = file.getAbsolutePath();
Log.d(tag, "New Path:" + newPath);
if(file.exists()){
Log.d(tag, "File Exists");
Drawable d = Drawable.createFromPath(newPath);
setImage.setImageDrawable(d);
}else{
try{
Log.d(tag,"File didnt exist");
FileOutputStream out = new FileOutputStream(file);
frontImage.compress(Bitmap.CompressFormat.PNG, 90, out);
if(file.exists()){
Log.d(tag, "file exists now");
newPath = file.getAbsolutePath();
Drawable b = Drawable.createFromPath(newPath);
setImage.setImageDrawable(b);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
这就是我访问映像并尝试在重新启动时将其设置为ImageView()的方式
这似乎是获取图像和设置图像的最佳途径,但它不起作用。
最佳实践是什么?我如何让它以我需要的方式运行
非常感谢您的帮助
savedInstanceState
有不同的用途
存储实例状态(捆绑):
在终止活动之前调用此方法,以便在
在将来的某个时间返回时,它可以恢复其状态
而且,在你的特殊情况下,它甚至可能不是必需的。单击按钮,您正在更改片段,而不是重新启动应用程序
从我看到的情况来看,你让用户创建了一张明信片:一面是图片(比如a面),另一面是信息(比如B面)。当应用程序启动时,可以看到A面。在某种程度上,您可以让用户从库中选择图像。我将假设onActivityResult(int,int,Intent)按预期工作,并将图像设置为ImageView-R.id.ImageView
。单击按钮时,视图将更改为B侧。再次单击按钮时,视图将更改为a侧,但用户选择的图像不存在
在activityResult(int,int,Intent)中可以做的一件事是:将图像的路径保存在SharedReferences中
SharedPreferences preferences;
final String PREFS = "your.application.name.prefs";
// Keyword to find the path
final String IMAGE_SELECTED_BY_USER = "image_selected_by_user";
// Use a default image when the user starts the app for the first time
// or if the retrieved path points to a deleted image etc.
final String PATH_TO_A_DEFAULT_IMAGE = "path_to_a_default_image"
@Override
protected void onCreate(Bundle savedInstanceState) {
....
....
preferences = getActivity().getSharedPreferences(PREFS, 0);
imagePath = preferences.getString(IMAGE_SELECTED_BY_USER, PATH_TO_A_DEFAULT_IMAGE);
frontImageView = (ImageView) findViewById(R.id.imageView);
Drawable drawable = null;
if (new File(imagePath).exists()) {
drawable = Drawable.createFromPath(imagePath);
} else {
drawable = Drawable.createFromPath(PATH_TO_A_DEFAULT_IMAGE);
}
frontImageView.setImageDrawable(drawable);
getFragmentManager()
.beginTransaction()
.add(R.id.postcardFrame, new CardFrontFragment())
.commit();
....
....
}
在onActivityResult(int,int,Intent)中,保存图像路径:
if(file.exists()){
Log.d(tag, "File Exists");
Drawable d = Drawable.createFromPath(newPath);
setImage.setImageDrawable(d);
Editor editor = preferences.edit();
editor.putString(IMAGE_SELECTED_BY_USER, newPath);
editor.commit();
} else{
try{
Log.d(tag,"File didnt exist");
FileOutputStream out = new FileOutputStream(file);
frontImage.compress(Bitmap.CompressFormat.PNG, 90, out);
if (file.exists()) {
Log.d(tag, "file exists now");
newPath = file.getAbsolutePath();
Drawable b = Drawable.createFromPath(newPath);
setImage.setImageDrawable(b);
Editor editor = preferences.edit();
editor.putString(IMAGE_SELECTED_BY_USER, newPath);
editor.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
这样,当用户启动应用程序时,他/她将看到默认图像或先前选择的图像
其中savedInstanceState
将非常有用:假设您为用户提供了在B面上编写短消息的选项。现在,如果用户在编写消息时,将设备从横向旋转到纵向(反之亦然),他/她写的消息将消失,因为活动将被销毁并重新创建。要保存消息,请使用onSaveInstanceState(Bundle)
:
在轮换时,将调用活动的onCreate(Bundle)”。传递的捆绑包与
onSaveInstanceState(捆绑包)`中的捆绑包相同。要检索文本,请执行以下操作:
String savedString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.contains("Text_To_Save")) {
savedString = savedInstanceState.getString("Text_To_Save");
}
}
someEditText.setText(savedString);
}
您不应该检查NOTNULL吗?if(outState!=null)。我尝试了这两种方法,但仍然得到null指针错误@tool您是否尝试过完全删除if(outState==null)行?得到相同的错误@ToolWhere's the null exception:在this.fffragment或outState上?当调用onSave时,可能this.fffragment为null。这是一个令人惊讶的答案,它似乎得到了很多处理,但是我应该在哪里为保存的常量路径重新初始化ImageView?我在onCreate中尝试了它,在imageview.setDrawable(明信片.getString(imageConst,Null))上得到了一个空指针错误@vikram@KeeanoMartin谢谢您可以编辑您的问题并发布更新的代码吗?或者,我们可以聊天:。
if(file.exists()){
Log.d(tag, "File Exists");
Drawable d = Drawable.createFromPath(newPath);
setImage.setImageDrawable(d);
Editor editor = preferences.edit();
editor.putString(IMAGE_SELECTED_BY_USER, newPath);
editor.commit();
} else{
try{
Log.d(tag,"File didnt exist");
FileOutputStream out = new FileOutputStream(file);
frontImage.compress(Bitmap.CompressFormat.PNG, 90, out);
if (file.exists()) {
Log.d(tag, "file exists now");
newPath = file.getAbsolutePath();
Drawable b = Drawable.createFromPath(newPath);
setImage.setImageDrawable(b);
Editor editor = preferences.edit();
editor.putString(IMAGE_SELECTED_BY_USER, newPath);
editor.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("Text_To_Save", someEditText.getText().toString());
}
String savedString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.contains("Text_To_Save")) {
savedString = savedInstanceState.getString("Text_To_Save");
}
}
someEditText.setText(savedString);
}