简体   繁体   English

相机意图在某些设备中不起作用

[英]Camera Intent Not Working in Some Devices

In my app, there is a button which launches the camera intent and saves the captured image at given path. 在我的应用程序中,有一个按钮可启动相机意图并将捕获的图像保存在给定路径中。 It works perfectly fine in Nexus 5X emulator, but when I tried it in my S8 or S9 it does not open the camera nor does it show any error. 它在Nexus 5X模拟器中可以正常运行,但是当我在S8或S9中尝试过时,它不会打开相机,也不会显示任何错误。 How can I fix camera intent in my App 如何在我的应用程序中解决相机意图

private void PictureTakerAction()
    {
        Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(takePic.resolveActivity(getPackageManager()) != null)
        {
            File photoFile = null;
            photoFile = CreatePhotoFile();

            if(photoFile != null)
            {
                pathToFile = photoFile.getAbsolutePath();
                Uri photoUri = FileProvider.getUriForFile(AddItemActivity.this,"com.airtechsolutions.fileprovider",photoFile);
                takePic.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                startActivityForResult(takePic,1);
            }
        }
    }

private File CreatePhotoFile()
    {
        String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        //File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = null;

        try {
            image = File.createTempFile(name ,".jpg" , storageDir);
        } catch (Exception e) {
            Log.i("File Log", "Exception: " + e.toString());
        }

        return image;
    }

protected void onActivityResult(int requestCode,int resultCode, Intent data)
{   super.onActivityResult(requestCode,resultCode,data);

    if (requestCode == 1)
     {
       Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
       Image.setImageBitmap(bitmap);
     }
}

You have an error in your app. 您的应用程序有错误。 Which is that you are writing the temp file in a public directory. 这是您在公共目录中写入临时文件。

Storage Permissions 储存权限

From the documentation: 从文档中:

Every Android app runs in a limited-access sandbox. 每个Android应用程序都在访问受限的沙箱中运行。 If an app needs to use resources or information outside of its own sandbox, the app has to request the appropriate permission. 如果应用程序需要在其自己的沙箱之外使用资源或信息,则该应用程序必须请求适当的权限。 You declare that your app needs a permission by listing the permission in the app manifest and then requesting that the user approve each permission at runtime (on Android 6.0 and higher). 通过在应用清单中列出该许可,然后请求用户在运行时(在Android 6.0及更高版本上)批准每个许可,即可声明您的应用需要许可。

Permissions like using the Internet, etc. are not even required, because they are not considered dangerous. 甚至不需要使用Internet等权限,因为它们不被视为危险。 Permissions, however, to use the camera, storage, etc. are dangerous as if given without the users' permission, the app can invade the users' privacy. 但是,使用相机,存储设备等的许可很危险,就好像未经用户许可而授予该应用程序一样,也可能侵犯用户的隐私。

What you are doing is creating a temp file in a public directory, something that requires write/read permission. 您正在做的是在公共目录中创建一个临时文件,该文件需要具有写/读权限。 Before Android 6.0, you didn't have to request read/write permissions to read/write. 在Android 6.0之前的版本中,您无需请求读取/写入权限即可进行读取/写入。 But if you read the above, it says that dangerous permissions are needed to be requested for Android 6.0 and higher, explaining why your code works on some devices. 但是,如果您阅读以上内容,则说明Android 6.0及更高版本需要请求危险的权限,这说明了为什么您的代码可在某些设备上运行。

Instead of storing it in a specific directory, just do this if the temp file you are creating is seen only by you: 如果您只看到正在创建的临时文件,请执行以下操作,而不是将其存储在特定目录中:

String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

try {
    return File.createTempFile(name ,".jpg");
} catch (Exception e) {
    Log.i("File Log", "Exception: " + e.toString());
    return null;
}

Also, try doing this: 另外,尝试这样做:

takePic.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

This will create the file in a private directory, meaning that you don't need to request storage permissions. 这将在专用目录中创建文件,这意味着您不需要请求存储权限。 You can continue with the code now. 您现在可以继续执行代码。

I have solved the issue :) It was done in 2 steps 我已经解决了这个问题:)分两步完成

Step 1:- 第1步:-

in My Java file I change the storage process from 在我的Java文件中,我从

File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
File image = null;
image = File.createTempFile(name ,".jpg", storageDir);

to

File outputDir = getApplicationContext().getCacheDir();
File image = null;
image = File.createTempFile(name ,".jpg", outputDir);

And the Step 2 was that I change my File Path (res/xml/file_paths) 步骤2是我更改了文件路径(res / xml / file_paths)

from

<Paths >
<external-path
    name = "logixtraders_images"
    path = "Pictures"/> 
</Paths>

to

<paths>
  <external-path
    name="external"
    path="." />
  <external-files-path
    name="external_files"
    path="." />
  <cache-path
    name="cache"
    path="." />
  <external-cache-path
    name="external_cache"
    path="." />
  <files-path
    name="files"
    path="." />
</paths>

It worked Perfectly fine for me 对我来说很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM