枫之缘
2018-05-10 07:36
采纳率: 60%
浏览 2.1k

小米手机插入系统图库找不到图片

// 文件插入系统图库
try {
MediaStore.Images.Media.insertImage(instance.getContentResolver(), path, filename, null);
// 最后通知图库更新
instance.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

其他手机都行就是小米,图库里找不到图片,求大神解答

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • javaxiaobai18 2018-05-10 08:00
    已采纳

    安卓把照片加载到图库 (Add the Photo to a Gallery)
    http://developer.android.com/intl/zh-cn/training/camera/photobasics.html

    官方的指导已经说的很明确了,但有几个需要注意的小地方,稍不留神就会犯错

    1 官方文档不严谨
    在创建文件的最后有这样一句

    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
    1
    2
    3
    这是添加到相册的代码

    private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);//这一句有错误
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    因为先前已经增加了file:”,所以再一次解析uri就会有问题,应当Uri.fromfile时候直接传入保存的图片就行

    2 小米手机无法添加到相册
    注意在创建文件的时候的这一句

    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    1
    2
    小米手机会发现在sd卡下找不到Picture这一个文件夹,默认照相机是放在sdcard下DCIM/Camera这一个文件夹,这个文件夹对外是可读可写的,也就是说我们应用通过调用系统相机,可以把图片放在这里

    3 全部代码
    //点击这个按钮开启系统相机,并且添加到相册
    public void photo(View view){
    try {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    photoFile = createImageFile();
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
    startActivityForResult(takePictureIntent, 1);
    //startActivity(takePictureIntent);

    } catch (IOException e) {
        e.printStackTrace();
    }
    

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // super.onActivityResult(requestCode, resultCode, data);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);

    Uri contentUri = Uri.fromFile( photoFile );
    System.out.println(contentUri.toString());
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
    

    }

    private File createImageFile() throws IOException {
    // Create an image file name
    /*
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
    imageFileName,
    ".jpg",
    storageDir
    );

    */
    String timeStamp = new SimpleDateFormat("HHmmss").format(new Date());
    File image = new File(Environment.getExternalStorageDirectory(),"DCIM/Camera/"+timeStamp+".jpg");
    
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath =  image.getAbsolutePath();
    return image;
    

    }

    点赞 评论

相关推荐 更多相似问题