>

부분적으로 카테고리별로 구성된 이미지 데이터베이스를 만드는 앱을 작성하려고합니다. 카메라에 액세스하려는 의도를 사용하는 데 문제가 있습니다. 카메라에 들어가서 이미지를 찍을 수 있지만 앱이 잘못된 활동으로 돌아갑니다. (NewWord 활동 대신 MainActivity). 관련이있는 경우 Google의 "조망이있는 방"예제 프로젝트에서이를 구축하고 있습니다. 미리 감사드립니다!

public class MainActivity extends AppCompatActivity {
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
private WordViewModel mWordViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true); //Line to improve performance
    final WordListAdapter adapter = new WordListAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new GridLayoutManager(this,4)); //Changed this
    // Get a new or existing ViewModel from the ViewModelProvider.
    mWordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);
    //Unload Intent to get Category
    String chosenCat = getIntent().getExtras().getString("CAT_KEY");
    //File DIR
    final String DIR = getFilesDir().toString();
   // final String DIR  = Environment.getExternalStoragePublicDirectory(
     //       Environment.DIRECTORY_PICTURES).toString();
    //final String picsDIR = getDir("Pictures", 0).toString();
    //Check what category and load
    // The onChanged() method fires when the observed data changes and the activity is
    // in the foreground.
    if(chosenCat.equals("BASIC")) {
        mWordViewModel.getBasicCatWords().observe(this, new Observer<List<Word>>() {
            @Override
            public void onChanged(@Nullable final List<Word> words) {
                // Update the cached copy of the words in the adapter.
                adapter.setWords(words);
                Toast.makeText(
                        getApplicationContext(),
                        "Category 1",
                        Toast.LENGTH_LONG).show();
            }
        });
    } //If Basic
    else if(chosenCat.equals("ALL")) {
        mWordViewModel.getAllWords().observe(this, new Observer<List<Word>>() {
            @Override
            public void onChanged(@Nullable final List<Word> words) {
                // Update the cached copy of the words in the adapter.
                adapter.setWords(words);
                Toast.makeText(
                        getApplicationContext(),
                        DIR,
                        Toast.LENGTH_LONG).show();
            }
        });
    } //Else if all
    else if(chosenCat.equals("PLAY")) {
        mWordViewModel.getPlayCatWords().observe(this, new Observer<List<Word>>() {
            @Override
            public void onChanged(@Nullable final List<Word> words) {
                // Update the cached copy of the words in the adapter.
                adapter.setWords(words);
                Toast.makeText(
                        getApplicationContext(),
                        "Category 2",
                        Toast.LENGTH_LONG).show();
            }
        });
    } //Else if play
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, NewWordActivity.class);
            startActivityForResult(intent, NEW_WORD_ACTIVITY_REQUEST_CODE);
        }
    });
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        //Word word = new Word(data.getStringExtra(NewWordActivity.WORD_REPLY));
        Word word = new Word(data.getStringExtra(NewWordActivity.WORD_REPLY),data.getStringExtra(NewWordActivity.CAT_REPLY));
        mWordViewModel.insert(word);
    } else {
        Toast.makeText(
                getApplicationContext(),
                R.string.empty_not_saved,
                Toast.LENGTH_LONG).show();
    }
}

}

주요 활동 (위)은 의도를 사용하여 NewWordActivity로 이동합니다. (플로팅 액션 버튼을 누를 때) NewWordActivity (아래)에서 "사진 찍기"버튼을 누를 때 추가 의도가 카메라에 들어가는 데 사용됩니다.

  /**
   * Activity for entering a word.
   */
public class NewWordActivity extends AppCompatActivity implements 
AdapterView.OnItemSelectedListener {
public static final String WORD_REPLY = "com.example.android.wordlistsql.WORD";
public static final String CAT_REPLY = "com.example.android.wordlistsql.CAT";
public static final String PIC_REPLY = "com.example.android.wordlistsql.PIC";
static final int REQUEST_IMAGE_CAPTURE = 2;
//DIR for Pictures
//final String picsDIR = getDir("Pictures", 0).toString();
private EditText mEditWordView;
String wordCategory;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_word);
    mEditWordView = findViewById(R.id.edit_word);
    //Category Spinner
    Spinner categories = findViewById(R.id.category_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.word_categories, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    categories.setAdapter(adapter);
    categories.setOnItemSelectedListener(this);
    //Choose Image Button
    final Button choosePicButton = findViewById(R.id.button_getPic);
    choosePicButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            TakePictureIntent();
            finish();
        }
    });
    //Save Button
    final Button saveButton = findViewById(R.id.button_save);
    saveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent replyIntent = new Intent();
            if (TextUtils.isEmpty(mEditWordView.getText())) {
                setResult(RESULT_CANCELED, replyIntent);
            } else {
                String word = mEditWordView.getText().toString();
                replyIntent.putExtra(WORD_REPLY, word);
                replyIntent.putExtra(CAT_REPLY, wordCategory);
                setResult(RESULT_OK, replyIntent);
            }
            finish();
        }
    });
}
//Methods for Category Spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    wordCategory = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
//Methods for Picture Selector
private void TakePictureIntent(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //MediaStore.ACTION_IMAGE_CAPTURE
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Toast.makeText(
            getApplicationContext(),
            "I made it to result",
            Toast.LENGTH_LONG).show();
    //Preview Image
    ImageView picPreview = (ImageView)findViewById(R.id.picPreview);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        picPreview.setImageBitmap(imageBitmap);
    }
}

}

  • 답변 # 1

    finish(); 제거  와이즈 비즈  클릭시. 이렇게 보일 것입니다

    choosePicButton
    
    
    choosePicButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { TakePictureIntent(); } });

관련 자료

  • 이전 css - 스크롤 할 때 페이지 요소 뒤를 이동하는 Wordpress 사이트 헤더
  • 다음 Python - 파이썬 - numpy : 배열의 모든 홀수 행과 모든 짝수 열을 동시에 선택할 수있는 방법