Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 藍牙:修改檔案接收路徑的方法

編輯:關於 Android 程式設計

需要修改的檔案:

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

相關程式碼片段:

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();
            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()) {
            return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
        }
        stat = new StatFs(base.getPath());
    } else {
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_ERROR_NO_SDCARD);
    }
    // 檢查目標檔案系統是否有足夠空間保存檔案,並預留一些餘裕
    if (stat.getBlockSize() * ((long) stat.getAvailableBlocks() - 4) < length) {
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_ERROR_SDCARD_FULL);
    }
    filename = choosefilename(hint);
    if (filename == null) {
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
    }
    String extension = null;
    int dotIndex = filename.lastIndexOf(".");
    if (dotIndex < 0) {
        if (mimeType == null) {
            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)) {
        // 檔案名過長時進行截斷,避免超過 Linux 255 字元上限
        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);
        }
    }
    filename = base.getPath() + File.separator + filename;
    // 生成唯一檔案名,建立檔案並返回
    String fullfilename = chooseUniquefilename(filename, extension);
    if (!safeCanonicalPath(fullfilename)) {
        // 若第二次檢查失敗,則拒絕傳輸
        return new BluetoothOppReceiveFileInfo(BluetoothShare.STATUS_FILE_ERROR);
    }
    if (fullfilename != null) {
        try {
            new FileOutputStream(fullfilename).close();
            int index = fullfilename.lastIndexOf('/') + 1;
            // 更新顯示名稱
            if (index > 0) {
                String displayName = fullfilename.substring(index);
                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) {
            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();
        // 檢查 canonical path 是否完整(大小寫敏感)
        if (!canonicalPath.startsWith(sDesiredStoragePath)) {
            return false;
        }
        return true;
    } catch (IOException ioe) {
        return false;
    }
}

修改方法要點:

root 代表儲存的路徑根目錄。若要改存到外部儲存,修改此處的賦值即可;若要修改子目錄,則調整 base 的拼接賦值。

特別提醒:如果改動了 root 或 base,務必同步為 sDesiredStoragePath 重新賦值,保持一致,否則 safeCanonicalPath() 的判斷會失敗。務必讓完整路徑合法,否則將無法接收檔案。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved