【问题标题】:The Camera Intent is simply not working相机意图根本不起作用
【发布时间】:2011-12-15 17:27:03
【问题描述】:
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

【问题讨论】:

  • “它不起作用”并不是对正在发生的事情的非常准确的陈述。你看到了什么?
  • 相机只是继续将图片保存在默认目录中,使用默认名称而不是我想要的目录。
  • 你看过这个问题的答案了吗? stackoverflow.com/questions/2729267/android-camera-intent

标签: android android-camera android-camera-intent


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多