在Android中将我的应用注册为相机应用

时间:2017-04-12 07:05:46

标签: android

我从android: how to register my app as "camera app"获得了帮助,但仍然没有正常工作

我已尝试使用联络应用将照片添加到我现有的已保存联系人,其URI将生成如下

content://com.google.android.contacts.files/my_cache/ContactPhoto-IMG_20170412_123034.jpg

但我无法访问该联系人应用的目录,因此我无法将照片保存在该URI上,如果我这样做,我将获得FIleNotFoundException

Uri saveUri = (Uri) getIntent().getExtras().getParcelable(MediaStore.EXTRA_OUTPUT);

if (saveUri != null)
{
    // Save the bitmap to the specified URI (use a try/catch block)
    outputStream = getContentResolver().openOutputStream(saveUri); <-----File Not Found Exception 

宣言是

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 <activity
            android:name=".CameraSelfieActivity"
            android:clearTaskOnLaunch="true"
            android:icon="@drawable/icon_chooser_selfie"
            android:label="My Camera"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.media.action.IMAGE_CAPTURE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>

Logcat是

java.io.FileNotFoundException: /my_cache/ContactPhoto-IMG_20170417_205819.jpg: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:452)
    at java.io.FileInputStream.<init>(FileInputStream.java:76)
    at com.example.CameraSelfieBaseHelper.copyFileToUri(CameraSelfieBaseHelper.java:391)
    at com.example.CameraSelfieBaseHelper.processImageReady(CameraSelfieBaseHelper.java:364)
    at com.example.CameraSelfieBaseHelper.onImageReady(CameraSelfieBaseHelper.java:266)
    at com.example.captureImage(CmGLSV.java:50)
    at com.example.surface.CmGLSV.onDrawFrame(CmGLSV.java:135)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1535)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:438)

但默认的相机应用程序

实现了相同的功能

任何人都会建议如何解决这个问题

谢谢,

2 个答案:

答案 0 :(得分:2)

问题是您的应用没有直接覆盖联系人应用程序外部管理的文件(联系人照片)所需的安全权限。

您可以采取的措施是:

一个。添加必要的权限以通过ContentResolver API读取/写入联系人数据

<uses-spermission
                android:name="android.permission.WRITE_CONTACTS" />

<uses-permission
            android:name="android.permission.READ_CONTACTS" />

您可以找到more information on interacting with Contacts data via the Android Developer

湾使用新照片识别要更新的联系人的ID

℃。通过它的ContentResolver API更新联系人的照片

There's this example open-source project on github that does just that

答案 1 :(得分:1)

不要使用File,请使用getContentResolver().openOutputStream(uri)来获取OutputStream