Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓相机意图_Android_Android Camera_Android Camera Intent - Fatal编程技术网

Android 安卓相机意图

Android 安卓相机意图,android,android-camera,android-camera-intent,Android,Android Camera,Android Camera Intent,我需要推送一个意图到默认相机应用程序,使其拍摄照片,保存并返回URI。有什么方法可以做到这一点吗?试试我发现的以下方法 试试这个代码 Intent photo= new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(photo, CAMERA_PIC_REQUEST); 我花了好几个小时才把它修好。代码几乎是复制粘贴而来,只是有一点不同 在AndroidManif

我需要推送一个意图到默认相机应用程序,使其拍摄照片,保存并返回URI。有什么方法可以做到这一点吗?

试试我发现的以下方法

试试这个代码

Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
                    startActivityForResult(photo, CAMERA_PIC_REQUEST);

我花了好几个小时才把它修好。代码几乎是复制粘贴而来,只是有一点不同

AndroidManifest.xml
上请求此权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后在一个
onClick
中触发此
Intent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.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
        Log.i(TAG, "IOException");
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }
}
添加以下支持方法:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
然后接收结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使它工作的是
MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(mCurrentPhotoPath))
,它与源代码不同。原始代码给了我一个
FileNotFoundException

试试下面我在这里找到的

如果您的应用程序以M及以上为目标,并声明为使用未授予的摄像头权限,则尝试使用此操作将导致SecurityException

EasyImage.openCamera(活动,int类型)

@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
handleActivityResult(请求代码、结果代码、数据、this、新的DefaultCallback(){
@凌驾
public void onImagePickerError(异常e,EasyImage.ImageSource源,int类型){
//一些错误处理
}
@凌驾
公共无效onImagesPicked(列出图像文件,EasyImage.ImageSource源,int类型){
//处理图像
返回的图像(图像文件);
}
});
}

我找到了一个非常简单的方法。使用按钮打开它,使用
单击
侦听器启动函数
openc()
,如下所示:

String fileloc;
private void openc()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = null;
    try 
    {
        f = File.createTempFile("temppic",".jpg",getApplicationContext().getCacheDir());
        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {               
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(profile.this, BuildConfig.APPLICATION_ID+".provider",f));
            fileloc = Uri.fromFile(f)+"";
            Log.d("texts", "openc: "+fileloc);
            startActivityForResult(takePictureIntent, 3);
        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 3 && resultCode == RESULT_OK) {
        Log.d("texts", "onActivityResult: "+fileloc);
        // fileloc is the uri of the file so do whatever with it
    }
}

您可以使用
uri
location字符串执行任何操作。例如,我将其发送到图像裁剪器以裁剪图像。

谢谢,但拍摄的照片没有保存到设备,因此我在Uri=Uri.parse(data.toURI())中获得FileNotFoundException;bitmap=android.provider.MediaStore.Images.Media.getBitmap(contentResolver,uri);您是说您拍摄的图片没有按照您的选择存储在手机/设备上,还是发生了什么事情,即使您让它将图像存储在手机/设备上,它也会表现得好像没有保存一样?我是说,图片不会自动存储在设备上,而是在onActivityResult()中以位图的形式返回。不过,我找到了一个解决方案,我会在回答中提到。这是更好的答案。顶部答案会保存图像的不必要副本。这在用户画廊的应用程序中显示为两个图像,而不是一个,大多数人都认为它是一个bug。这很好,尽管您可能需要:私有静态int TopyPoint=1;不适用于GALAXY S手机:(为什么是硬编码的“android.media.action.IMAGE_CAPTURE”。它可能在某些手机上不起作用。有没有标准?可能是关于Intent.action_GET_内容的?kilaka,MediaStore.action_IMAGE_CAPTURE您可能需要在android>M中使用
文件提供商
。请看,这篇文章由于质量太差而被自动标记为低质量。)只有代码。你介意通过添加一些文本来扩展它来解释它是如何解决问题的吗?对我来说也是。我想你为我节省了几个小时。你知道为什么Android开发代码会出现IO异常吗?请参阅此库中的以下链接。无法设置照片的输出路径,而且在我看来也存在一些错误
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
        @Override
        public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
            //Some error handling
        }

        @Override
        public void onImagesPicked(List<File> imagesFiles, EasyImage.ImageSource source, int type) {
            //Handle the images
            onPhotosReturned(imagesFiles);
        }
    });
}
String fileloc;
private void openc()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = null;
    try 
    {
        f = File.createTempFile("temppic",".jpg",getApplicationContext().getCacheDir());
        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {               
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(profile.this, BuildConfig.APPLICATION_ID+".provider",f));
            fileloc = Uri.fromFile(f)+"";
            Log.d("texts", "openc: "+fileloc);
            startActivityForResult(takePictureIntent, 3);
        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 3 && resultCode == RESULT_OK) {
        Log.d("texts", "onActivityResult: "+fileloc);
        // fileloc is the uri of the file so do whatever with it
    }
}