无法访问Google Glass上应用内拍摄的照片

时间:2014-11-01 18:57:41

标签: android android-camera google-glass imgur

我在这里与谷歌眼镜开发混淆了!我的应用目前正在使用内置相机功能和GDK网站上的示例代码(https://developers.google.com/glass/develop/gdk/camera)拍照。当我拍照时,它看起来像正在拍照,但是当我尝试将它上传到imgur服务器(使用他们的API)时,我得到了一个FileNotFound异常。此外,当我尝试使用Android Studio的文件资源管理器时,我似乎无法在文件路径中找到任何图像。它似乎没有创建文件,或者我以某种方式访问​​错误的路径。我能做错什么?

使用相机的代码:

    public void startRecog(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG,"Got to onActivity");
        Log.i(TAG,"Request code: " + requestCode + ", Result code: " + resultCode + ", what it wants: " + RESULT_OK);
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            Log.i(TAG,"Got inside the IF");
            String picturePath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
          //  String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);

            Log.i(TAG,"The real path: " + picturePath);
            processPictureWhenReady(picturePath);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void processPictureWhenReady(final String picturePath) {
        final File pictureFile = new File(picturePath);

        if (pictureFile.exists()) {
            // The picture is ready; process it.
            Log.i(TAG,"Got in from the picture processing");
            new ImgurUploadTask(Uri.parse(picturePath), this).execute();
        } else {
            // The file does not exist yet. Before starting the file observer, you
            // can update your UI to let the user know that the application is
            // waiting for the picture (for example, by displaying the thumbnail
            // image and a progress indicator).

            final File parentDirectory = pictureFile.getParentFile();
            FileObserver observer = new FileObserver(parentDirectory.getPath(),
                    FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
                // Protect against additional pending events after CLOSE_WRITE
                // or MOVED_TO is handled.
                private boolean isFileWritten;

                @Override
                public void onEvent(int event, String path) {
                    if (!isFileWritten) {
                        // For safety, make sure that the file that was created in
                        // the directory is actually the one that we're expecting.
                        File affectedFile = new File(parentDirectory, path);
                        isFileWritten = affectedFile.equals(pictureFile);

                        if (isFileWritten) {
                            stopWatching();

                            // Now that the file is ready, recursively call
                            // processPictureWhenReady again (on the UI thread).
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    processPictureWhenReady(picturePath);
                                }
                            });
                        }
                    }
                }
            };
            observer.startWatching();
        }
    }

我得到的错误:

11-01 14:34:43.281  10449-10449/com.example.cerveau.recognizeplaces I/RecogPlaces﹕ Got to onActivity
11-01 14:34:43.281  10449-10449/com.example.cerveau.recognizeplaces I/RecogPlaces﹕ Request code: 100, Result code: -1, what it wants: -1
11-01 14:34:43.281  10449-10449/com.example.cerveau.recognizeplaces I/RecogPlaces﹕ Got inside the IF
11-01 14:34:43.281  10449-10449/com.example.cerveau.recognizeplaces I/RecogPlaces﹕ The real path: /storage/emulated/0/storage/emulated/0/thumbnail_cache/t_thumb_20141101_143439_397.jpg
11-01 14:34:43.281  10449-10449/com.example.cerveau.recognizeplaces I/RecogPlaces﹕ Got in from the picture processing
11-01 14:34:43.288  10449-10704/com.example.cerveau.recognizeplaces E/ImgurUploadTask﹕ could not open InputStream
    java.io.FileNotFoundException: No content provider: /storage/emulated/0/thumbnail_cache/t_thumb_20141101_143439_397.jpg
            at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1049)
            at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
            at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
            at com.example.cerveau.recognizeplaces.ImgurUploadTask.doInBackground(ImgurUploadTask.java:32)
            at com.example.cerveau.recognizeplaces.ImgurUploadTask.doInBackground(ImgurUploadTask.java:16)
            at android.os.AsyncTask$2.call(AsyncTask.java:302)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

是的,我认为我在AndroidManifest中设置了正确的权限......

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />

谢谢你们!我一直在为玻璃开发的每一步都陷入困境,这让我感到沮丧,无所不在。我真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

new ImgurUploadTask(Uri.parse(picturePath)

这是你的问题。您无法在存储路径上使用Uri.parse(例如&#34; / storage / emulated / 0 / thumbnail_cache ...&#34;),因为它不合格。

像这样创建Uri:

Uri.fromFile(pictureFile)

这将输出以&#34; file://&#34;。

开头的有效Uri
相关问题