文件未添加到图库 android [英] File not added to gallery android

查看:34
本文介绍了文件未添加到图库 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作的这个应用程序正在拍照并将其保存到 SD 卡,但图片没有显示在图库中...我的代码有问题吗

This app i am making is taking pictures and saving it to sdcard but the images are not being shown in gallery...Is there something wrong with my code

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

图像正在图像视图中显示并保存到所需目录,但现在显示在图库中最后这是添加到图库的代码

The image is being shown in the imageview as well as saved to the desired directory but now shown in gallery And finally this is the code for adding to 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);
}

这篇关于文件未添加到图库 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆