Android - 捕获图像后更新文件夹显示

时间:2012-04-22 10:24:39

标签: android android-intent android-widget android-camera

我是android的新手。现在,我正在使用以下方式进行图像捕捉功能:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );

问题是在我拍摄照片后,新拍摄的照片不会显示在图像显示页面上。

有没有人知道哪里有代码可以帮助我刷新我的android或我需要做的任何步骤,以便我的图像显示页面可以在我拍摄新照片后更新?

任何帮助都会非常感激,非常感谢你们。

更新的答案: 我用这个,可能这可以帮助别人:

                mScanner = new MediaScannerConnection(
                Camera.this,
                new MediaScannerConnection.MediaScannerConnectionClient() {
                    public void onMediaScannerConnected() {
                        mScanner.scanFile(outputFileUri.getPath(), null /* mimeType */);
                    }

                    public void onScanCompleted(String path, Uri uri) {
                        //we can use the uri, to get the newly added image, but it will return path to full sized image
                        //e.g. content://media/external/images/media/7
                        //we can also update this path by replacing media by thumbnail to get the thumbnail
                        //because thumbnail path would be like content://media/external/images/thumbnail/7
                        //But the thumbnail is created after some delay by Android OS
                        //So you may not get the thumbnail. This is why I started new UI thread
                        //and it'll only run after the current thread completed.
                        if (path.equals(outputFileUri.getPath())) {
                            mScanner.disconnect();
                            //we need to create new UI thread because, we can't update our mail thread from here
                            //Both the thread will run one by one, see documentation of android  
                            Camera.this
                            .runOnUiThread(new Runnable() {
                                public void run() {

                                }
                            });
                        }
                    }
                });
        mScanner.connect();

3 个答案:

答案 0 :(得分:4)

Sally,你的意思是说你拍照后,当你看到你知道该文件所在的目录时,你在画廊或文件管理器中看不到它吗?

如果是这样,您需要像这样运行媒体扫描程序:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

...其中uri是照片的uri,因为你已经知道了,虽然你可以使用目录的uri而不是更容易(虽然它更慢 - 如果目录包含许多文件或者可能非常慢)嵌套目录)。

答案 1 :(得分:1)

你应该使用以下代码拍摄照片::

Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(),(cal.getTimeInMillis()+".jpg"));
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selectedImageUri = Uri.fromFile(file);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
startActivityForResult(i, CAMERA_RESULT);

和活动结果你可以使用这些代码:::

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAMERA_RESULT:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), selectedImageUri);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}

答案 2 :(得分:0)

您可以使用以下方式刷新活动:

Intent myIntent = getIntent();
finish();
startActivity(myIntent);