修改文件:

packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppReceiveFileInfo.java

相关代码片段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
public static BluetoothOppReceiveFileInfo generateFileInfo(Context context, int id) {
    ContentResolver contentResolver = context.getContentResolver();
    Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
    String filename = null, hint = null, mimeType = null;
    long length = 0;
    Cursor metadataCursor = null;
    try {
        metadataCursor = contentResolver.query(contentUri, new String[] {
            BluetoothShare.FILENAME_HINT, BluetoothShare.TOTAL_BYTES, BluetoothShare.MIMETYPE
            }, null, null, null);
    } catch (SQLiteException e) {
        if (metadataCursor != null) {
            metadataCursor.close();
        }
        metadataCursor = null;
        Log.e(Constants.TAG, "generateFileInfo: " + e);
    } catch (CursorWindowAllocationException e) {
        metadataCursor = null;
        Log.e(Constants.TAG, "generateFileInfo: " + e);
    }
    if (metadataCursor != null) {
        try {
            if (metadataCursor.moveToFirst()) {
                hint = metadataCursor.getString(0);
                length = metadataCursor.getLong(1);
                mimeType = metadataCursor.getString(2);
            }
        } finally {
            metadataCursor.close();
            if (V) Log.v(Constants.TAG, "Freeing cursor: " + metadataCursor);
            metadataCursor = null;
        }
    }
    File base = null;
    StatFs stat = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String root = Environment.getExternalStorageDirectory().getPath();
        base = new File(root + Constants.DEFAULT_STORE_SUBDIR);
        if (!base.isDirectory() && !base.mkdir()) {
            if (D) Log.d(Constants.TAG, "Receive File aborted - can't create base directory "
                        + base.getPath());
            return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
        }
        stat = new StatFs(base.getPath());
    } else {
        if (D) Log.d(Constants.TAG, "Receive File aborted - no external storage");
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_ERROR_NO_SDCARD);
    }
    /*
     * Check whether there's enough space on the target filesystem to save
     * the file. Put a bit of margin (in case creating the file grows the
     * system by a few blocks).
     */
    if (stat.getBlockSize() * ((long)stat.getAvailableBlocks() - 4) < length) {
        if (D) Log.d(Constants.TAG, "Receive File aborted - not enough free space");
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_ERROR_SDCARD_FULL);
    }
    filename = choosefilename(hint);
    if (filename == null) {
        // should not happen. It must be pre-rejected
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
    }
    String extension = null;
    int dotIndex = filename.lastIndexOf(".");
    if (dotIndex < 0) {
        if (mimeType == null) {
            // should not happen. It must be pre-rejected
            return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
        } else {
            extension = "";
        }
    } else {
        extension = filename.substring(dotIndex);
        filename = filename.substring(0, dotIndex);
    }
    if ((filename != null) && (filename.getBytes().length > OPP_LENGTH_OF_FILE_NAME)) {
      /* Including extn of the file, Linux supports 255 character as a maximum length of the
       * file name to be created. Hence, Instead of sending OBEX_HTTP_INTERNAL_ERROR,
       * as a response, truncate the length of the file name and save it. This check majorly
       * helps in the case of vcard, where Phone book app supports contact name to be saved
       * more than 255 characters, But the server rejects the card just because the length of
       * vcf file name received exceeds 255 Characters.
       */
      try {
          byte[] oldfilename = filename.getBytes("UTF-8");
          byte[] newfilename = new byte[OPP_LENGTH_OF_FILE_NAME];
          System.arraycopy(oldfilename, 0, newfilename, 0, OPP_LENGTH_OF_FILE_NAME);
          filename = new String(newfilename, "UTF-8");
      } catch (UnsupportedEncodingException e) {
          Log.e(Constants.TAG, "Exception: " + e);
      }
      if (D) Log.d(Constants.TAG, "File name is too long. Name is truncated as: " + filename);
    }
    filename = base.getPath() + File.separator + filename;
    // Generate a unique filename, create the file, return it.
    String fullfilename = chooseUniquefilename(filename, extension);
    if (!safeCanonicalPath(fullfilename)) {
        // If this second check fails, then we better reject the transfer
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
    }
    if (V) Log.v(Constants.TAG, "Generated received filename " + fullfilename);
    if (fullfilename != null) {
        try {
            new FileOutputStream(fullfilename).close();
            int index = fullfilename.lastIndexOf('/') + 1;
            // update display name
            if (index > 0) {
                String displayName = fullfilename.substring(index);
                if (V) Log.v(Constants.TAG, "New display name " + displayName);
                ContentValues updateValues = new ContentValues();
                updateValues.put(BluetoothShare.FILENAME_HINT, displayName);
                context.getContentResolver().update(contentUri, updateValues, null, null);
            }
            return new BluetoothOppReceiveFileInfo(fullfilename, length, new FileOutputStream(
                    fullfilename), 0);
        } catch (IOException e) {
            if (D) Log.e(Constants.TAG, "Error when creating file " + fullfilename);
            return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
        }
    } else {
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
    }
}
private static boolean safeCanonicalPath(String uniqueFileName) {
    try {
        File receiveFile = new File(uniqueFileName);
        if (sDesiredStoragePath == null) {
            sDesiredStoragePath = Environment.getExternalStorageDirectory().getPath() +
                Constants.DEFAULT_STORE_SUBDIR;
        }
        String canonicalPath = receiveFile.getCanonicalPath();
        // Check if canonical path is complete - case sensitive-wise
        if (!canonicalPath.startsWith(sDesiredStoragePath)) {
            return false;
        }
        return true;
    } catch (IOException ioe) {
        // If an exception is thrown, there might be something wrong with the file.
        return false;
    }
}

修改方法:

root 就是存储的根目录。

如果需要将其修改之外部存储,修改此处的赋值即可。

如果需要修改子目录,修改base的拼接赋值即可。

此处修改需要注意,如果修改root或者base,则需要给sDesiredStoragePath重新赋值,使其一致,否则会导致safeCanonicalPath()判断会失败。
需要使完整的路径合法,否则无法接收文件。

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

转载于:https://www.cnblogs.com/rabbit-bunny/p/4195028.html

Android Bluetooth 文件接收路径修改方法相关推荐

  1. delphi打印html文件路径,Delphi获取文件名、不带扩展名文件名、文件所在路径、上级文件夹路径的方法...

    1.获取不带扩展名的文件名方法,利用ChangeFileExt函数修改传入参数的扩展为空,并不会对文件本身产生变更. ChangeFileExt(ExtractFileName('D:\KK\Test ...

  2. Android获取文件夹路径

    Android获取文件夹路径 应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的. 大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中. ...

  3. Java 查看文件绝对路径,JAVA获取文件绝对路径的方法

    本文实例讲述了JAVA获取文件绝对路径的方法.分享给大家供大家参考.具体实现方法如下: /** * 获取一个类的class文件所在的绝对路径. 这个类可以是JDK自身的类,也可以是用户自定义的类,或者 ...

  4. dede修改mysql,Dedecms(织梦)程序MySQL修复表和文章路径修改方法

    做网站是我们经常会遇到被一些小问题困扰的.其实有些问题非常的简单,通过百度搜索一下都是可以解决的. 最近模板网遇到两个用户的dedecms的问题,一个涉及到MySQL修复表,另一个就是文章路径修改,相 ...

  5. php算出文件相对路径,php计算两个文件相对路径的方法

    本文实例讲述了php计算两个文件相对路径的方法.分享给大家供大家参考.具体如下: 一.问题: 写一个php函数算出两个文件的相对路径.例如$a="/a/b/c/d/e.php"; ...

  6. Xcode增加头文件搜索路径的方法

    Xcode增加头文件搜索路径的方法 以C++工程为例:在Build Settings 页面中的Search Paths一节就是用来设置头文件路径.相关的配置项用红框框起来了,共有三个配置项: Head ...

  7. android蓝牙传文件开发,Android Bluetooth文件传输

    [实例简介] Android Bluetooth文件的引入和传输,可使用两台设备,一个做客户端一个做服务端,传输文件,显示传送进度. [实例截图] [核心代码] Bluetooth └── Bluet ...

  8. 此电脑下的默认7个文件夹路径修改

    此电脑下的默认7个文件夹路径修改 此电脑下的默认文件夹可能并不是我们想要的,修改文件夹的路径: 将该文件夹的内容转移 右击对应文件夹,选择属性 点击移动选择至想要指向的文件夹即可.

  9. windows服务启动路径修改方法

    ** windows服务启动路径修改方法 1.进入服务,查看路径,[开始]=>[运行]=>[services.msc] 2.进入注册表,修改服务路径[开始]=>[运行]=>[r ...

最新文章

  1. Gym迎来首个完整环境文档,强化学习入门更加简单!
  2. 因 Redis Key 命令不规范,导致熬了一个通宵才把Key删完了!
  3. mark一下总是记混的重定向与转发的区别
  4. 01-利用思维导图梳理JavaSE-Java语言基础
  5. 【Android 高性能音频】hello-oboe 示例解析 ( Oboe 源代码依赖 | CMakeList.txt 构建脚本分析 | Oboe 源代码构建脚本分析 )
  6. gitlab远程提交
  7. 利用rpm包搭建lamp环境及论坛的创建
  8. python中的深拷贝与浅拷贝
  9. JavaScript--fullPage.js插件
  10. SPI 读取不同长度 寄存器_[读书笔记]《计算机科学速成课》—6 寄存器和内存
  11. 通过在jquery中添加函数发送ajax请求来加载数据库数据,以json的格式发送到页面...
  12. Try using .loc[row_indexer,col_indexer] = value instead
  13. [JNI] 开发基础(6)字符串相关操作
  14. 2019年程序员薪资报告,网友:年薪20万只是起薪?
  15. Spring Kafka Transaction
  16. php实现 三角形_HTML纯CSS绘制三角形(各种角度)
  17. Aladdin推出软件智能卡和一次性密码认证解决方案
  18. 天原笔记(3)气旋与反气旋
  19. 小议关键字del与实例方法__del__(self)
  20. bh1750采集流程图_基于BH1750光照强度数据采集系统的设计

热门文章

  1. java super()方法_Java super关键字的使用方法详解
  2. 超长整数相加 c语言类,二个超长正整数的相加
  3. tcpip运输层不同的两个协议_TCP/IP-运输层-你需要知道的运输层概念
  4. 数据库的数据在硬盘上吗
  5. java为何是跨平台语言,以及java如何运行
  6. 044_Properties工具类
  7. 011_CSS子元素选择器
  8. linux c语言 glibc 错误 munmap,Linux内存分配小结--malloc、brk、mmap
  9. js里面字符数字和数字相加_「译」5 个奇怪的只会在 JS 里才发生的趣事
  10. 传智播客韩顺平老师2011ssh实战项目校内网的数据库设计32张表全解