文件未添加到库android

文件未添加到库android,android,android-intent,android-imageview,android-gallery,android-mediascanner,Android,Android Intent,Android Imageview,Android Gallery,Android Mediascanner,我正在制作的这个应用程序正在拍摄照片并将其保存到SD卡,但图像没有显示在gallery中…我的代码是否有问题 public void takepicture(View view){ try{ String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { //To

我正在制作的这个应用程序正在拍摄照片并将其保存到SD卡,但图像没有显示在gallery中…我的代码是否有问题

public void takepicture(View view){
     try{
           String state = Environment.getExternalStorageState();
             if (Environment.MEDIA_MOUNTED.equals(state)) {
                    //To store public files
                File directory=new File(Environment.getExternalStorageDirectory()
                        , "Myapp Pictures");                        
                    if(!directory.exists())
                        directory.mkdir();
                // Create an image file name
                    String timeStamp = 
                        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    String imageFileName = "Img" + timeStamp + ".jpg";
                        file=new File(directory, imageFileName);
                   if(!file.exists())
                       file.createNewFile();                    
             }
    }catch(Exception e){
    e.getCause();   
    }
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    mImageUri=Uri.fromFile(file);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(takePictureIntent, actioncode);          
    }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {                             
            if(requestCode == actioncode){                  
                    if((file.length())==0){
                        file.delete();
                    }                   
            try{
                if(resultCode==Activity.RESULT_OK){ 


                         Bitmap photo = (Bitmap) data.getExtras().get("data");
                         imageView.setImageBitmap(photo);   
                    }

                    galleryAddPic(file.toString()); 
                }
            }catch(Exception e){
                e.getCause();
            }
        }
    }
图像显示在imageview中,并保存到所需目录,但现在显示在gallery中 最后,这是添加到gallery的代码

public void galleryAddPic(String file) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(file);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }
试试这个:

public void galleryAddPic(String file) {
    File f = new File(file);
    Uri contentUri = Uri.fromFile(f);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
    sendBroadcast(mediaScanIntent);
}
找到最好的解决方案必须看到