The Camera Intent is simply not working

2019-09-01 19:10发布

问题:

public class CameraintentActivity extends Activity {

    String _path, sliderpather;
    Button button;
    Intent intent;
    Uri outputFileUri;
    File file;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button)findViewById(R.id.button1);

        _path = Environment.getExternalStorageDirectory().getName()  + File.separatorChar +  "make_machine_example.jpg";
        file = new File( _path );
        outputFileUri = Uri.fromFile( file );

        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        button.setOnClickListener(
            new OnClickListener()
            {
                public void onClick(View v)
                {
                    startActivityForResult( intent, 0 );
                }
            }
        );
    }
}

I want my app so it pulls the camera, takes a picture, and saves the picture into my desired directory but it is not working...

and yes the AndroidManifest.xml contains the following permissions

CAMERA WRITE_INTERNAL_STORAGE WRITE_EXTERNAL_STORAGE

回答1:

You don't sent a file:/// URI for parameter MediaStore.EXTRA_OUTPUT. You need a content resolver URI. From the javadoc:

"The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video."

Read up on ContentResolvers to see how to use that mechanism to store info.



回答2:

Use below code to pass the correct uri to the camera activity

File f = new File(Environment.getExternalStorageDirectory(), "hello.jpg");
Uri outputFileUri = Uri.fromFile( f );
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );        

Currently you are doing it wrong way. As @Sean Owen had suggest you, you are sending the wrong uri.