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

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

// 文件插入系统图库
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;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示