私はスコープを1つ下げるために質問を完全に書き直しました。フラグメントのフリップとロス画像
私はカードのように(左、右)フリップする2つの断片があります。前方の断片が消えると、それは裏を示す。ボタンをもう一度クリックすると、前面に再びフリップされますが、前面のフラグメントのImageViewは消えてしまいます。
私は、選択した画像のデータを保存するさまざまな方法を試しました。
- 保存断片onSaveInstanceState
これは私にNULLポインタを与えるので、私は、私は一度作成され、より一定の何かを必要と考え出し。
- は、だから今、私はSDカードに画像を保存すると
これを選んだ私は仕事とちょうどパスを確認し、そのフロントに裏返すか、アクティビティが再作成された場合にそれをつかむと考えました。
//flip card
final Button cardBackButton = (Button) findViewById(R.id.cardBackButton);
cardBackButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
flipCard();
});
flipCard方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_postcard_activity);
//UI call
frontImageView = (ImageView) findViewById(R.id.imageView);
Log.d(tag, "onCreate() Instance:" + savedInstanceState);
//fragment start
if (savedInstanceState == null) {
Log.d(tag,"Instance Null");
getFragmentManager()
.beginTransaction()
.add(R.id.postcardFrame, new CardFrontFragment())
.commit();
if(!mShowingBack){
Log.d(tag,"Not showing back");
if(newPath != null && newPath != ""){
Log.d(tag, "entered new path, not empty");
Drawable drawable = Drawable.createFromPath(newPath);
Log.d(tag, "Should be setting saved image.");
frontImageView.setImageDrawable(drawable);
}
}
}
else
{
mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);
Log.d(tag, "Instance is not Null");
}
フリップボタンは、リスナーをクリックします:ここで
は、いくつかのコード
のonCreate()である
private void flipCard()
{
Log.d(tag2, "Log after flipCard:" + mShowingBack);
if(mShowingBack)
{
//Flip to front
flipFront();
return;
}
// Flip to back
flipBack();
}
は、私は彼らのフォトギャラリーから画像をonActivityResult設定
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();
}
これは私がイメージにアクセスすると再起動()これは、最適なルートでのように思える
if(imageSet == true){
if(newPath != null && newPath != ""){
ImageView view = (ImageView) findViewById(R.id.imageView);
Drawable drawable = Drawable.createFromPath(newPath);
view.setImageDrawable(drawable);
}
}
に私のImageViewのにそれを設定しようとしています方法ですイメージを取得し、それを設定するが、それは動作しません。 ベストプラクティスとはどのようにして必要な方法でそれを実現するには?
多くのご協力をいただきありがとうございます。
NOT NULLをチェックするべきではありませんか? if(outState!= null)です。 – Tool
私は両方を試みましたが、まだNull Pointer Errorが発生します。 @ツール – Keeano
if(outState == null)行を完全に削除しようとしましたか? – Tool