相机意图根本不起作用

时间:2011-12-15 17:27:03

标签: android android-camera

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

我想要我的应用程序,所以它拉相机,拍照,并将图片保存到我想要的目录,但它不起作用...

是的,AndroidManifest.xml包含以下权限

CAMERA WRITE_INTERNAL_STORAGE WRITE_EXTERNAL_STORAGE

2 个答案:

答案 0 :(得分:0)

您没有为参数MediaStore.EXTRA_OUTPUT发送file:/// URI。您需要内容解析器URI。来自javadoc:

“Intent-extra的名称,用于表示用于存储请求的图像或视频的内容解析器Uri。”

阅读ContentResolver以了解如何使用该机制存储信息。

答案 1 :(得分:0)

使用以下代码将正确的uri传递给相机活动

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

目前你做错了。正如@Sean Owen建议你的那样,你发错了uri。