资源与环境
from ascript.android.system import R
常量
Android上下文
- 获取上下文 Android Context ,在android系统中,上下文有很多的作用.
打开意图
获取资源
等
R.context
工程名称
- 获取当前工程名称
R.name
当前小程序ID
- 获取当前运行的小程序ID
如果当前运行的非线上小程序,返回0
R.id
当前APP包名
- 获取当前程序的包名 可根据此id判断是否为打包小程序
R.package_name
方法
获取相对路径
获取 当前文件的相对路径
- 函数
R.rel(__file__,*rel_paths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
rel_paths | *str | 是 | 获取相对于当前文件的相对路径,不能以 “/”开头,否则就是绝对路径.,如果传入多个值返回列表 |
- 返回值
str : 文件地址
#获取上级目录下res/img/logo.png
path = R.rel(__file__,"../res/img/logo.png")
print(path)
#获取同级目录的文件绝对路径
path = R.rel(__file__, "res/img/logo.png")
print(path)
#获取同级目录的文件绝对路径,返回多个路径的列表
paths = R.rel(__file__, "res/img/logo.png","res/img/1.png")
print(paths)
获取SD卡目录
获取手机SD卡目录
- 函数
R.sd(*child_paths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
child_paths | *str | 否 | 子文件路径 默认:None,返回SD卡目录 ,如果多个值返回列表 |
- 返回值
str|str列表 : SD卡地址 或 SD卡和子目录的拼接地址
- 示例
# 获取SD卡根路径
from ascript.android.system import R
path = R.sd()
print(path)
# 获取SD卡 下的 1.png 文件路径
from ascript.android.system import R
path = R.sd("1.png")
print(path)
# 获取SD卡下的 1.png,2.png 文件路径,这将返回一个列表
from ascript.android.system import R
paths = R.sd("1.png","2.png")
print(paths)
获取工程目录
获取当前运行的工程目录
- 函数
R.root(*child_paths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
child_paths | int | 否 | 子文件路径 默认:None,返回当前工程路径 ,传入多个子路径,则返回列表 |
- 返回值
str或str列表 : 根目录 或 根目录和子目录的拼接地址
- 示例
# 获取系统根目录
from ascript.android.system import R
path = R.root()
print(path)
# 获取根目录下的子文件1.txt
from ascript.android.system import R
path = R.root("1.txt")
print(path)
# 获取根目录下的子文件1.txt,2.txt 这将返回一个列表
from ascript.android.system import R
paths = R.root("1.txt","2.txt")
print(paths)
获取res目录
获取 **当前工程/res/**资源目录路径
- 函数
R.res(*childpaths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
childpaths | int | 否 | 相对于res的子目录,默认,返回资源目录,传入多个子路径,则返回列表 |
- 返回值
str或s t r : 文件地址
- 示例
# 获取资源文件根目录
from ascript.android.system import R
path = R.res()
print(path)
# 获取资源文件
from ascript.android.system import R
path = R.res("img/a.png")
print(path)
# 获取多个资源文件,这将返回一个列表
from ascript.android.system import R
paths = R.res("img/a.png","img/b.png")
print(paths)
获取img目录
当前工程/res/img
- 函数
R.img(*childpaths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
childpaths | int | 否 | 相对于res/img的子目录,默认,返回res/img目录,传入多个值则返回列表 |
- 返回值
str或str列表 : 文件地址
- 示例
# 获取工程下的图片资源根目录
from ascript.android.system import R
path = R.img()
print(path)
# 获取工程下的图片资源
from ascript.android.system import R
path = R.img("logo.png")
print(path)
# 获取工程下的图片资源列表
from ascript.android.system import R
paths = R.img("logo.png","2.png")
print(paths)
获取ui目录
当前工程/res/ui
- 函数
R.ui(*childpaths):
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
childpaths | int | 否 | 相对于res/ui/childpath,默认,返回res/ui,传入多个值则返回列表 |
- 返回值
str或str列表 : 文件地址
- 示例
# 获取工程下的UI资源路径
from ascript.android.system import R
path = R.ui()
print(path)
# 获取工程目录下的 UI 子文件路径
from ascript.android.system import R
path = R.ui("a.html")
print(path)
# 获取工程目录下的 UI 子文件路径列表
from ascript.android.system import R
paths = R.ui("a.html","b.html")
print(paths)