为相机意图创建URI时应用崩溃

时间:2019-03-21 18:00:12

标签: android android-intent android-camera2

我正在实现从相机应用程序拍摄图像并将其存储在应用程序存储中的功能。此活动“ NewWordActivity”处理用于添加到会议室数据库的条目的集合。

尝试创建URI传递给相机意图时,应用程序崩溃(下面在“ TakePictureIntent”方法中突出显示)。我是否错误地实现了意图?谢谢!

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

}

这是发生崩溃的方法。

//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);
        }

    }
}

这是相机意图的onActivityResult方法。该应用程序永远不会使相机意图将其放置在此处。

@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);
    }
}

这处理用于URI引用的文件对象的创建。

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;

}

这是错误:

---------崩溃开始 E / AndroidRuntime:致命异常:main     流程:com.example.android.roomwordssample,PID:4728     java.lang.IllegalArgumentException:无法找到包含/storage/emulated/0/Android/data/com.example.android.roomwordssample/files/Pictures/word_20190321_1250344218377806003302351.jpg的已配置根目录         在android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider.java:712)         在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)         在com.example.android.roomwordssample.NewWordActivity.TakePictureIntent(NewWordActivity.java:135)         在com.example.android.roomwordssample.NewWordActivity.access $ 000(NewWordActivity.java:46)         在com.example.android.roomwordssample.NewWordActivity $ 1.onClick(NewWordActivity.java:79)         在android.view.View.performClick(View.java:6294)         在android.view.View $ PerformClick.run(View.java:24770)         在android.os.Handler.handleCallback(Handler.java:790)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6494)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

0 个答案:

没有答案