android - 相机 Intent 复制到自定义文件夹延迟

标签 android google-glass google-gdk

我在谷歌眼镜中有一个相机应用程序,我正在做的是将图像从默认目录复制到我的自定义目录。起初,我以为它不起作用,但是当我关闭玻璃并再次打开它时,所有图片都存在。这就是为什么我意识到它正在工作,但需要重新启动才能发生更改。

这是我的代码。

public class CameraActivity extends Activity {

// Camera activity request codes
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
private Uri fileUri;

private Slider mSlider;
private Slider.Indeterminate mIndeterminate;
private String TAG = "TAG";
private String pid;
private static final int MEDIA_TYPE_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    pid = intent.getStringExtra("patientid");
    takePicture();
}

/**
 * Launching camera app to capture image
 */
private void takePicture() {

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}


/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
           if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
                View view1 = new CardBuilder(getBaseContext(), CardBuilder.Layout.MENU)
                .setText("Please Wait...")
                .setIcon(R.drawable.ic_photo_50)
                .getView();

                setContentView(view1);
                // Set the view for the Slider
                mSlider = Slider.from(view1);
                mIndeterminate = mSlider.startIndeterminate();
            String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
                processPictureWhenReady(picturePath);

        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            View view2 = new CardBuilder(getBaseContext(), CardBuilder.Layout.MENU)
            .setText("Cancelled")
            .setIcon(R.drawable.ic_no_50)
            .getView();

            setContentView(view2);
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            am.playSoundEffect(Sounds.DISMISSED);
            finish();

        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry Failed to capture image", Toast.LENGTH_SHORT)
                    .show();

            finish();
        }

    super.onActivityResult(requestCode, resultCode, data);

}

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

    if (pictureFile.exists()) {
        File from = new File(pictureFile.toString());
        File to = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/ProfilePicture_"+ pid +".jpg");
        Log.d(TAG,to.toString());
        try {
            FileUtils.copyFile(from, to, true);            
            Log.d(TAG,"NO ERROR");
        } catch (IOException e) {            
            Log.d(TAG,"ERROR");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /* The picture is ready; process it.
        mIndeterminate.hide();
        String isImage = "Yes";
        Intent i = new Intent(CameraActivity.this, UploadActivity.class);
        i.putExtra("filePath", picturePath);
        i.putExtra("isImage", isImage);
        startActivity(i);*/
        finish();
    } 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();
    }
}

我错过了什么吗?请帮我。谢谢。

最佳答案

我不确定您所说的“我认为它不起作用”是什么意思,但您可能需要发送广播以使图库知道您添加了新图像。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

请参阅Image, saved to sdcard, doesn't appear in Android's Gallery apphow to update gallery after moving photo programmatically? .

您还可以从 shell 检查图像是否存在。

注意:正如 Ralfh 评论的那样, 尝试使用 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 而不是 Intent.ACTION_MEDIA_MOUNTED。

关于android - 相机 Intent 复制到自定义文件夹延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357199/

相关文章:

android - 支持旧设备

php - 无法在 Google Glass 上播放视频(附件选项或捆绑选项)

java - 使用底部导航栏选项卡的不同背景颜色

android - 当 TabActivity 被弃用时我该怎么办?

android-studio - 已安装 Glass Development Kit 但 Android Studio 未找到

android - SurfaceView 正在交换红色和蓝色的 Google Glass 相机颜色

android - 将 Google Maps API 实现到 Google Glass 时出错

google-glass - Google Glass 可以使用英语以外的语言进行搜索吗?

android - ListView 第一项不可见/更新 Android

google-mirror-api - 您能否允许用户回复卡片而不是在时间轴中显示带有回复文本的卡片?