gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:13:33 24 4
gpt4 key购买 nike

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

这是我的代码。

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 - Camera Intent 复制到自定义文件夹延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com