【Android9.0】【ftell】相机拍照保存到sdcard中的图片无法显示

2024-03-27 15:48

本文主要是介绍【Android9.0】【ftell】相机拍照保存到sdcard中的图片无法显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【现象】

  • 相机设置存储为外部存
  • 相机拍完照之后,相机相册无法显示图片
  • 重启手机或者重新mount sdcard就可以显示

【背景】

  • 由于Android 9.0,apk如果需要向sdcard中保存数据只有apk本身sdcard路径下才有权限操作,sdcard其他路径下无法操作。于是camera apk那边采用了DocumentFile方式绕过此处的权限管控。
  • 采用此方法之后,相册 apk采用底层C语言fseek(filp, 0L, SEEK_END)和ftell(filp)方式获取文件size为0才无法显示图片

【分析】

  • 怀疑camera产生图片文件之后没有close掉
    • 进入adb mode,使用“lsof 文件”查看无其他进程在使用,说明图片文件已经被close了
  • 写一个C测试程序方便采用ftell测试图片文件
    • /external/test/Android.mk
      LOCAL_PATH := $(call my-dir)
      include $(CLEAR_VARS)LOCAL_MODULE := testLOCAL_CFLAGS := \-Wall -Werror \-Wno-sometimes-uninitialized \-Wno-unused-parameter \-Wno-unused-function \LOCAL_SRC_FILES := \test.c \$(NULL)include $(BUILD_EXECUTABLE)
    • /external/test/test.c
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <fcntl.h>
      #include <linux/fs.h>
      #include <sys/ioctl.h>
      #include <unistd.h> // for close
      #include <linux/dm-ioctl.h>
      #include <libgen.h>
      #include <sys/param.h>
      #include <sys/mount.h>
      #include <errno.h>
      #include <linux/kdev_t.h>int main(int argc, char** argv) {FILE *pFile = NULL;long fSize_cur = 0;long fSize_stop = 0;int ret = -1;//long acount = 0;char buf[2];printf("[wxl]: Starting ftell........\n");pFile = fopen(argv[1], "rb+");if (pFile == NULL) {printf("[wxl]: Error opening file\n");} else {//while (!feof(pFile)) {memset(buf, 0 , sizeof(buf));fread(buf, sizeof(buf), 1, pFile);printf("[wxl]: fread buf[0] %x\n",buf[0]);printf("[wxl]: fread buf[1] %x\n",buf[1]);fSize_cur = ftell(pFile);printf("[wxl]: ftell fSize_cur %ld\n",fSize_cur);ret = fseek(pFile, 0L, SEEK_CUR);printf("[wxl]: fseek SEEK_CUR return %d\n",ret);memset(buf, 0 , sizeof(buf));fread(buf, sizeof(buf), 1, pFile);printf("[wxl]: fread buf[0] %x\n",buf[0]);printf("[wxl]: fread buf[1] %x\n",buf[1]);fSize_cur = ftell(pFile);printf("[wxl]: ftell fSize_cur %ld\n",fSize_cur);ret = fseek(pFile, 0L, SEEK_END);printf("[wxl]: fseek SEEK_END return %d\n",ret);fSize_stop = ftell(pFile);printf("[wxl]: ftell fSize_stop %ld\n",fSize_stop);//acount ++;//}}//printf("[wxl]: acount = %ld\n",acount);printf("[wxl]: %d %s\n",argc,argv[1]);	fclose(pFile);pFile = NULL;}
    • mmm /external/test编译test可执行程序
    • adb push到手机,进入adb mode,执行test测试程序发现:
      • 获取/storage/76EE-1CE6/DCIM/100MEDIA/IMAG0001.jpg文件size为0(实际是fseek(filp, 0L, SEEK_END)定位到文件头)
      • 获取/mnt/media_rw/76EE-1CE6/DCIM/100MEDIA/IMAG0001.jpg文件size正常(与ls -l查看文件size一致)
      • 修改test测试程序源代码,采用fread从头读到尾(用!feof(pFile)判断到文件头)来计算文件size,可以正常获取文件size
      • 将/storage/76EE-1CE6/DCIM/100MEDIA/IMAG0001.jpg文件在adb mode下采用“cp”命令拷贝到/storage/76EE-1CE6/路径,用test测试程序可以正常获取/storage/76EE-1CE6/路径下文件size
      • 通过手机设置->存储->sdcard->找到IMAG0001.jpg文件->拷贝到/storage/76EE-1CE6/路径下(apk本身拷贝方法),再用test测试程序获取/storage/76EE-1CE6/路径下文件size为0
  • 说明图片文件本身是没问题的,只是fseek(filp, 0L, SEEK_END)不能定位到文件末尾
  • 在adb mode下,使用“mount”命令查看mount信息
  • /dev/block/vold/public:179,1 on /mnt/media_rw/76EE-1CE6 type vfat (rw,dirsync,nosuid,nodev,noexec,noatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)
    /mnt/media_rw/76EE-1CE6 on /mnt/runtime/default/76EE-1CE6 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=1015,mask=6)
    /mnt/media_rw/76EE-1CE6 on /storage/76EE-1CE6 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=1015,mask=6)
    /mnt/media_rw/76EE-1CE6 on /mnt/runtime/read/76EE-1CE6 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=18)
    /mnt/media_rw/76EE-1CE6 on /mnt/runtime/write/76EE-1CE6 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=18)
    • /mnt/media_rw/76EE-1CE6/是底层设备直接mount点,为vfat文件系统
    • /storage/76EE-1CE6是/mnt/media_rw/76EE-1CE6/以sdcard虚拟文件系统的mount点,为sdcard虚拟文件系统
  • vfat文件系统可以,sdcard文件系统不行,很可能是sdcard文件系统有问题
  • 写一个ko驱动程序,打印图片文件kernel中的inode部分信息
    • c文件
      /** Copyright (C) 2010 HTC, Inc.** This software is licensed under the terms of the GNU General Public* License version 2, as published by the Free Software Foundation, and* may be copied, distributed, and modified under those terms.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the* GNU General Public License for more details.**/#include <linux/kernel.h>
      #include <linux/init.h>
      #include <linux/module.h>
      #include <linux/fs.h>
      #include <linux/proc_fs.h>
      #include <asm/uaccess.h>
      #include <linux/seq_file.h>static int __init print_fail_file_struct_init(void)
      {struct file *pFile = NULL;char fileName[126] = "/storage/76EE-1CE6/DCIM/100MEDIA/IMAG0001.jpg";//char fileName[126] = "/storage/45AE-1AE4/111.jpg";printk("<wxl>:\n");printk("<wxl>: Starting print fail file.\n");pFile = filp_open(fileName,  O_RDWR | O_SYNC, 0);if (IS_ERR(pFile)) {printk("<wxl>: unable to open file: %s\n", fileName);return 0;}printk("<wxl>: file name:%s                   \n", (pFile->f_path).dentry->d_iname);printk("<wxl>: file inode magic:%li           \n", pFile->f_inode->i_sb->s_magic);printk("<wxl>: file inode uid:%d              \n", (pFile->f_inode->i_uid).val);printk("<wxl>: file inode gid:%d              \n", (pFile->f_inode->i_gid).val);printk("<wxl>: file inode count:%d            \n", (pFile->f_inode->i_count).counter);printk("<wxl>: file inode write count:%d      \n", (pFile->f_inode->i_writecount).counter);printk("<wxl>: file inode flags:%d            \n", pFile->f_inode->i_flags);printk("<wxl>: file inode bytes:%d            \n", pFile->f_inode->i_bytes);printk("<wxl>: file inode blocks:%ld          \n", pFile->f_inode->i_blocks);printk("<wxl>: file inode state:%ld           \n", pFile->f_inode->i_state);printk("<wxl>: file inode size:%lld           \n", pFile->f_inode->i_size);printk("<wxl>: file inode i_size_read:%lld    \n", i_size_read(pFile->f_inode));printk("<wxl>: file mode:%d                   \n", pFile->f_mode);printk("<wxl>: file count:%ld                 \n", (pFile->f_count).counter);printk("<wxl>: file flags:%d                  \n", pFile->f_flags);printk("<wxl>: file loff_t:%lld               \n", pFile->f_pos);filp_close(pFile, NULL);return 0;}static void __exit print_fail_file_struct_exit(void)
      {
      }module_init(print_fail_file_struct_init);
      module_exit(print_fail_file_struct_exit);
      MODULE_DESCRIPTION("WXL Print File Struct Interface");
      MODULE_VERSION("1.0");
      MODULE_LICENSE("GPL v2");

      以上code主要获取文件属性

    • Makefile添加“obj-m += print_fail_file_struct.o”
    • 编译生成的ko文件,adb push到/data路径下,insmod安装驱动,打印信息发现sdcard文件系统路径下的图片文件inode中的i_size和i_block为0,而vfat文件系统路径下的图片文件inode中的i_size和i_block正常
  • 查看fseek通过vfs调用sdcard文件系统中的llseek函数的实现(应该首要check sdcard文件系统llseek函数),通过kernel/fs/sdcard/file.c文件可知llseek调用通用的llseek函数(kernel/fs/read_write.c文件中generic_file_llseek函数),发现fseek传入whence为“SEEK_END”时计算文件size,是通过文件inode中的i_size来计算偏移量的。
  • 说明就是文件inode中的i_size不对导致的。vfat文件系统i_size正常,而sdcard文件系统(基于vfat文件系统再次mount出来的一个虚拟文件系统)i_size却为0,是不是两个文件系统文件inode系统没有同步,什么时候同步inode信息?
  • 查看kernel/fs/sdcard/file.c,发现sdcard的write函数中,write完数据会同步底层vfat文件系统中的inode信息
    static ssize_t sdcardfs_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
    {int err;struct file *lower_file;struct dentry *dentry = file->f_path.dentry;struct inode *inode = d_inode(dentry);/* check disk space */if (!check_min_free_space(dentry, count, 0)) {pr_err("No minimum free space.\n");return -ENOSPC;}lower_file = sdcardfs_lower_file(file);err = vfs_write(lower_file, buf, count, ppos);/* update our inode times+sizes upon a successful lower write */if (err >= 0) {if (sizeof(loff_t) > sizeof(long))inode_lock(inode);fsstack_copy_inode_size(inode, file_inode(lower_file));fsstack_copy_attr_times(inode, file_inode(lower_file));if (sizeof(loff_t) > sizeof(long))inode_unlock(inode);}return err;
    }

    code中lower_file是底层文件指针,此函数直接是对lower_file进行vfs_write

  • 既然有同步动作,为什么却没有同步呢?除非此code没有被调用
  • 添加debug log,发现确实没有被调用。那数据是采用什么方式写入呢?(测试发现没有走sdcard文件中的write、mmap等可以写入的方式)
  • 与负责camera同事沟通了解DocumentFile只是为了获取sdcard的权限,写数据仍然采用OutputStream类进行写操作。看来apk侧没什么突破口了。
  • 向kernel/fs/路径下的相关文件中添加debug log,分别抓取apk采用DocumentFile类向/mnt/media_rw/76EE-1CE6/DCIM/100MEDIA/路径创建文件并写入时dmesg log,以及抓取apk采用正常File类向sdcard apk路径(/storage/76EE-1CE6/Android/data/com.xxx)创建文件并写入时dmesg log
  • 对比发现正常方式会走sdcard中的write函数,而采用DocumentFile方式走vfat中的write(通用write)对数据进行写入,所以sdcard虚拟文件系统中文件inode没能同步底层vfat文件系统中文件inode。
  • 最终得到如规律:
    • /storage/76EE-1CE6/(sdcard文件系统)路径下创建一个空文件a.txt
    • mnt/media_rw/76EE-1CE6/(vfat文件系统)路径下对a.txt文件进行写入数据
    • 此时/storage/76EE-1CE6/a.txt文件i_size就为0
    • mnt/media_rw/76EE-1CE6/(vfat文件系统)路径下创建空文件b.txt,并直接对b.txt进行写入数据,此时不管/storage/76EE-1CE6/b.txt还是mnt/media_rw/76EE-1CE6/b.txt都正常
  • 这有可能是Google设计sdcard文件系统是未能考虑到一种场景
  • 自己在sdcard_flush函数中主动同步底层lower_file数据,在sdcard文件系统中的文件可以正常获取文件size,至于能不能正常做,还不清楚。

【总结】

根据以上分析数据,猜测采用DocumentFile流程应该是:

  • 先在sdcard文件系统创建文件a,此时并没有写入数据
  • 然后对vfat文件系统中文件a进行写入数据
  • 而此时另外一个进程(相册中的进程)直接读取sdcard文件系统中文件a的size,这时获取文件a的size为0.(相册那边是否可以把文件路径改为vfat文件系统路径,不知道是否有权限问题?)

以下是我对Linux文件系统操作流程的理解,如有不对的地方,还请帮忙提出多谢!

 

这篇关于【Android9.0】【ftell】相机拍照保存到sdcard中的图片无法显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/852631

相关文章

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

据阿谱尔APO Research调研显示,2023年全球髓内钉市场销售额约为4.7亿美元

根据阿谱尔 (APO Research)的统计及预测,2023年全球髓内钉市场销售额约为4.7亿美元,预计在2024-2030年预测期内将以超过3.82%的CAGR(年复合增长率)增长。 髓内钉市场是指涉及髓内钉制造、分销和销售的行业。髓内钉是一种用于整形外科手术的医疗器械,用于稳定长骨骨折,特别是股骨、胫骨和肱骨。髓内钉通常由不銹钢或钛等材料制成,并插入骨的髓管中,以在愈合过程中提供结构支

vue+elementUI下拉框联动显示

<el-row><el-col :span="12"><el-form-item label="主账号:" prop="partyAccountId" :rules="[ { required: true, message: '主账号不能为空'}]"><el-select v-model="detailForm.partyAccountId" filterable placeholder="

在服务器上浏览图片

@StarSky 2018-10-26 15:09 字数 15971 阅读 28 https://www.zybuluo.com/StarSky/note/1294871 来源 2018-09-27 线上服务器安装 imgcat Tool   2018-09-27 线上服务器安装 imgcat 0. 准备文件:iterm2_shell_integration.bash1. 在有权限

Pycharm配置conda环境(解决新版本无法识别可执行文件问题)

引言: 很多小伙伴在下载最新版本的pycharm或者更新到最新版本后为项目配置conda环境的时候,发现文件夹目录中无法显示可执行文件(一般为python.exe),以下就是本人遇到该问题后试验和解决该问题的一些方法和思路。 一般遇到该问题的人群有两种,一种是刚入门对pycharm进行conda环境配置的小白(例如我),不熟悉相关环境配置的操作和过程,还有一种是入坑pycharm有段时间的老手

青龙面板之Ninja无法安装无法拉库问题解决

因为之前的Ninja库已经不能用了,甚至新找到的库也不能用了,好尴尬,这里使用线下版本进行安装。 ninja安装新方法,其是方法还是原来的,只不过Ninja的库原作者删了,没法直接git了,但是我找到了源码包,我们可以直接通过宝塔面板拖进去。 源码包地址: https://download.csdn.net/download/u012134073/24813485 备用地址: 链接: h

设置android返回键,保存和取得最高分

1.在.h中声明一些方法 virtual void keyBackClicked();           //Android返回键 bool isHaveSaveFile(); void getHighestHistoryScore(); 在.cpp中实现这个几个方法 void WelcomeLayer::keyBackClicked(

温湿度采集及OLED显示

目录 软件I2C和硬件I2C每隔2秒钟采集一次温湿度数据,显示到OLED上,同时通过串口发送到上位机的“串口助手”软件 软件I2C和硬件I2C "I2C"代表Inter-Integrated Circuit,是一种用于在数字电路之间进行通信的串行通信协议。软件I2C和硬件I2C是两种实现这种协议的方式。 软件I2C是通过软件来模拟I2C通信协议的实现方式。在这种情况下,微控制

el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

<div v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList" :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http: