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

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

问题描述

该应用程序,我想提出的拍照,并保存到SD卡,但图像没有在画廊正在显示...是不是有什么毛病我code

 公共无效takepicture(查看视图){
     尝试{
           字符串状态= Environment.getExternalStorageState();
             如果(Environment.MEDIA_MOUNTED.equals(州)){
                    //要存储公用文件
                文件目录=新的文件(Environment.getExternalStorageDirectory()
                        MYAPP图片);
                    如果(!directory.exists())
                        directory.mkdir();
                //创建一个图像文件名
                    字符串的timeStamp =
                        新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date());
                    字符串映像文件名称=图+ +的timeStamp.JPG;
                        文件=新的文件(目录,映像文件名称);
                   如果(!file.exists())
                       file.createNewFile();
             }
    }赶上(例外五){
    e.getCause();
    }
    意图takePictureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    mImageUri = Uri.fromFile(文件);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,mImageUri);
    startActivityForResult(takePictureIntent,动作code);
    }
        保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
            如果(要求code ==行动code){
                    如果((file.length())== 0){
                        file.delete();
                    }
            尝试{
                如果(结果code == Activity.RESULT_OK){
                         。位图的照片=(位图)data.getExtras()获得(数据);
                         imageView.setImageBitmap(照片);
                    }                    galleryAddPic(file.toString());
                }
            }赶上(例外五){
                e.getCause();
            }
        }
    }

图片在ImageView的被显示以及保存到指定的目录,但现在在画廊所示
最后,这是code添加到图库

 公共无效galleryAddPic(字符串文件){
        意图mediaScanIntent =新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        文件f =新的文件(文件);
        乌里contentUri = Uri.fromFile(F);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }


解决方案

试试这个:

 公共无效galleryAddPic(字符串文件){
    文件f =新的文件(文件);
    乌里contentUri = Uri.fromFile(F);
    意图mediaScanIntent =新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
    sendBroadcast(mediaScanIntent);
}

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

解决方案

Try this:

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天全站免登陆