相机意图根本不起作用

发布于 2024-12-21 08:12:43 字数 1221 浏览 0 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-12-28 08:12:43

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

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

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

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.

So要识趣 2024-12-28 08:12:43

使用下面的代码将正确的 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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文