1

I'm implementing functionality to take an image from the camera app and store it in app storage. This activity "NewWordActivity" handles the collection of entries for addition to a room database.

The app crashes when trying to create the URI to pass to the camera intent (highlighted below in the "TakePictureIntent" method). Am I implementing the intent incorrectly? Thanks!

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;
String wordPhotoDIR;

@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) {

}

This is the method where the crash occurs.

//Methods for Picture Selector
private void TakePictureIntent(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //MediaStore.ACTION_IMAGE_CAPTURE
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(
                    getApplicationContext(),
                    R.string.error_photo_file,
                    Toast.LENGTH_LONG).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            /*App Crashes Here*/

            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            /*.................*/

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }

    }
}

This is the onActivityResult Method for the camera intent. The app never makes it to the camera intent to make it here.

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

    //Preview Image
    ImageView picPreview = 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);
    }
}

This handles the creation of the file object for the URI to reference.

private File createImageFile() throws IOException{

    //Create Name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "word_" + timeStamp;
    File storageDIR = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName, //Prefix
        ".jpg",  //Suffix
        storageDIR    //Directory
    );

    //Save file and path
    wordPhotoDIR = image.getAbsolutePath();
    return image;

}

Here's the error:

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.roomwordssample, PID: 4728 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.android.roomwordssample/files/Pictures/word_20190321_1250344218377806003302351.jpg at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401) at com.example.android.roomwordssample.NewWordActivity.TakePictureIntent(NewWordActivity.java:135) at com.example.android.roomwordssample.NewWordActivity.access$000(NewWordActivity.java:46) at com.example.android.roomwordssample.NewWordActivity$1.onClick(NewWordActivity.java:79) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

0 Answers0