2015-08-26 86 views
-1

我的相機應用程序出現問題,當我拍照時,可以在圖像視圖中看到它,但是當我打開手機或關閉應用程序並重新打開它時,圖像就會消失。我能做些什麼來修復我的應用程序?

我的代碼 - >

public class semana1 extends Activity { 

    Button btnfoto1; 
    ImageView imgs1; 
    static final int CAM_REQUEST=1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.semana1); 
     btnfoto1= (Button) findViewById(R.id.btnfoto1); 
     imgs1= (ImageView) findViewById(R.id.imgs1); 
     btnfoto1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent int1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       File file=getfile(); 
       int1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
       startActivityForResult(int1,CAM_REQUEST); 

      } 
     }); 

    } 
    private File getFile() 
    { 
     File folder=new File("sdcard/Progress"); 

     if(!folder.exists()) 
     { 
      folder.mkdir(); 
     } 
     File image_file=new File(folder,"image1.jpg"); 
     return image_file; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     String path = "sdcard/Progress/image1.jpg"; 

     imgs1.setImageDrawable(Drawable.createFromPath(path)); 
    } 
} 
+0

到底有沒有,你就在你身邊做什麼來驗證的行爲和診斷它。 – JoxTraex

+0

@JoxTraex當我使用我的應用程序時,我把我的phne,圖像消失,但我可以在我的文件中看到它。 – spain

回答

0

上的應用程序的activity lifecycle閱讀起來。

現在指出你有點正確的方向。當您關閉應用程序時,系統會停止應用程序,並可能稍後將其取消。你的圖像消失的原因是你沒有實現一種方式讓你的應用程序保存之前的狀態。

因此,你應該執行:

public void onStop() { 
    super.onStop(); 
    // code where you tell the app to save the image 
} 

public void onDestroy() { 
    super.onDestroy(); 
    // code where you tell the app to save the image 
} 
+0

我在裏面放了什麼代碼? @TheRealRave – spain

+0

去圖。這是學習的最佳方式。 – TheRealRave

相關問題