2015-11-07 39 views
0

我想在用戶打開我的應用程序後啓動相機。如何將相機意圖設置爲主要活動

現在我有這個,它工作正常。當用戶啓動我的應用程序時,它會自動打開相機。但是,用戶在拍攝圖像後點擊「返回」按鈕,它會打開一個空白的活動。

如何讓它回到相機?

public class MainActivity extends AppCompatActivity { 

private static final int REQUEST_TAKE_PHOTO = 0; 

// The URI of photo taken with camera 
private Uri mUriPhotoTaken; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    takePhoto(); 
} 

// Deal with the result of selection of the photos and faces. 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     Uri imageUri; 
     if (data == null || data.getData() == null) { 
      imageUri = mUriPhotoTaken; 
     } else { 
      imageUri = data.getData(); 
     } 
     Intent intent = new Intent(MainActivity.this, Result.class); 
     intent.setData(imageUri); 
     startActivity(intent); 
    } 
} 

// Launch the camera to allow the user to take a photo 
public void takePhoto(){ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if(intent.resolveActivity(getPackageManager()) != null) { 
     // Save the photo taken to a temporary file. 
     File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     try { 
      File file = File.createTempFile("IMG_", ".jpg", storageDir); 
      mUriPhotoTaken = Uri.fromFile(file); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriPhotoTaken); 
      startActivityForResult(intent, REQUEST_TAKE_PHOTO); 
     } catch (IOException e) { 
      Log.d("ERROR", e.getMessage()); 
     } 
    } 
} 
} 

回答

0

試圖調用takePhoto()方法在onStart(),而不是onCreate(),然後調用finish()方法進入onStop()

0

試試吧。

 @Override 
     public void onBackPressed() { 
      super.onBackPressed(); 
      Intent intent = new Intent(this, ActivityYouWantToOpen.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      finish(); 
     } 
相關問題