2012-04-22 65 views
1

更新文件夹显示我是新到Android。现在,我用做拍摄功能:的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(); 
+0

我得到了答案: – Sally 2012-04-22 15:41:51

回答

4

莎莉,你的意思是你拍摄照片后,你没有看到它在画廊或文件管理器,当你看着你知道文件是目录?

如果是这样,你需要运行媒体扫描仪这样的:

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

...其中URI是照片的URI,既然你已经知道了,虽然你可以使用的URI如果这更容易,那么该目录会更容易(尽管速度较慢 - 如果目录包含许多文件或嵌套目录,则速度可能非常慢)。

+0

是的。好主意。我会尝试使用此代码。谢谢 – Sally 2012-04-22 15:38:44

+0

文件夹没有出现在画廊中 – nida 2015-01-21 10:11:07

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; 
} 
} 
} 
+0

您好,感谢您的回复。其实我可以拍照并将其保存在我的SD卡中。问题是,在拍摄新照片后,我有一个显示该目录图像的页面,但该页面未显示新捕获的照片。希望我明确提出我的问题,如果我的解释不清楚,我很抱歉。 :) – Sally 2012-04-22 14:18:28

+0

这是什么:_activity.getContentResolver() 它给出了一个错误。 – Yster 2012-11-25 10:51:49

0

你可以刷新你的活动:

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

嗨,诺丽,谢谢你的回复。但是,我可以知道我应该在哪里放置这些代码?在创建部分还是其他部分?因为当我把它放在我的代码中时,代码将会有无限循环。任何想法都将非常感激。谢谢 – Sally 2012-04-22 14:38:22