android_lesson_2
16lz
2021-12-04
android单元测试
1.首先在AndroidManifest.xml中加入下面红色代码<uses-library android:name="android.test.runner" /> <!-- append to application noe-->
<!-- append to manifest;com.example.myfirstapp == manifest.package -->
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.myfirstapp" />
public void test() throws Exception {
InputService inputService=new InputService();
Integer r=inputService.queryPort();
Assert.assertEquals(new Integer(1), r);
}
}
note:
无法导出文件:"Failed to pull selection",查看文件名是否存在中文、权限是否为rw
<!--申请操作sd卡读和删权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 申请操作sd卡写的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 文件操作:
package com.example.myfirstapp;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import android.content.Context;import android.os.Environment;public class FileOperatorService { private Context context; public void SetContext(Context context) { this.context = context; } public void write(String fileName, String content) throws Exception { FileOutputStream out = context.openFileOutput(fileName,context.MODE_PRIVATE); out.write(content.getBytes()); out.close(); } public String read(String fileName) throws Exception { FileInputStream reader = context.openFileInput(fileName); ByteArrayOutputStream memoryArr = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len; while ((len = reader.read(buf)) != -1) { memoryArr.write(buf, 0, len); } byte[] data = memoryArr.toByteArray(); memoryArr.close(); reader.close(); return new String(data); } public void write2sdCard(String fileName, String content) throws Exception { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdCardDir=Environment.getExternalStorageDirectory(); File file=new File(sdCardDir,fileName); FileOutputStream out=new FileOutputStream(file); out.write(content.getBytes()); out.close(); } } public String readFromSDCard(String fileName) throws Exception { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdCardDir=Environment.getExternalStorageDirectory(); File file=new File(sdCardDir,fileName); FileInputStream reader = new FileInputStream(file); ByteArrayOutputStream memoryArr = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len; while ((len = reader.read(buf)) != -1) { memoryArr.write(buf, 0, len); } byte[] data = memoryArr.toByteArray(); memoryArr.close(); reader.close(); return new String(data); } return null; }}
更多相关文章
- Cocos2d-x权限问题
- Android应用程序安装过程源代码分析(4)
- Android(安卓)密度转换实例
- Android中软键盘使用总结(一)
- Android(安卓)SDL移植版学习笔记
- Android(安卓)之经典源码
- Android(安卓)ViewHolder 优化 ListView
- Android平台mass storage相关代码
- [置顶] Android(安卓)2.3.5源代码 更新至android 6.0,可以下载,度