2015-02-06 98 views
2

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

这是我的代码。

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(); 
    } 
} 

我这么想吗?请帮帮我。谢谢。

回答

2

我不确定您的意思是“我认为它不工作”,但您可能需要发送广播让画廊知道您添加了新图像。

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。

+0

我认为这是行不通的,因为图像没有出现在它保存的文件夹中。但是当我关掉玻璃并打开它时,图像就在那里。 – rapidoodle 2015-02-06 02:36:20

+0

你有没有试过adb shell ls <目录>?在你重新启动之前有什么吗? – pt2121 2015-02-06 02:37:42

+0

我可以在哪里找到eclipse中的adb shell? – rapidoodle 2015-02-06 02:43:04