- 浏览: 325153 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
1:Fileservice
package cn.itcast.service; 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 FileService { private Context context; public FileService(Context context) { this.context = context; } /** * 以私有文件保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveToSDCard(String filename, String content) throws Exception{ File file = new File(Environment.getExternalStorageDirectory(), filename); FileOutputStream outStream = new FileOutputStream(file); outStream.write(content.getBytes()); outStream.close(); } /** * 以私有文件保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void save(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE); outStream.write(content.getBytes()); outStream.close(); } /** * 以追加方式保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveAppend(String filename, String content) throws Exception{// ctrl+shift+y / x FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用从该文件中读取内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveReadable(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用往该文件写入内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveWriteable(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用对该文件读和写 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveRW(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 读取文件内容 * @param filename 文件名称 * @return * @throws Exception */ public String readFile(String filename) throws Exception{ FileInputStream inStream = context.openFileInput(filename); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); return new String(data); } }
2:FileServiceTest
package cn.itcast.file; import cn.itcast.service.FileService; import android.test.AndroidTestCase; import android.util.Log; public class FileServiceTest extends AndroidTestCase { private static final String TAG = "FileServiceTest"; public void testSave() throws Throwable{ FileService service = new FileService(this.getContext()); service.save("itcast.txt", "www.itcast.cn"); } public void testReadFile() throws Throwable{ FileService service = new FileService(this.getContext()); String result = service.readFile("www.txt"); Log.i(TAG, result); } public void testSaveAppend() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveAppend("append.txt", ",www.csdn.cn"); } public void testSaveReadable() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveReadable("readable.txt", "www.sohu.com"); } public void testSaveWriteable() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveWriteable("writeable.txt", "www.sina.com.cn"); } public void testSaveRW() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveRW("rw.txt", "www.joyo.com"); } }
3:AceessOtherAppFileTest
package cn.itcast.other; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import android.test.AndroidTestCase; import android.util.Log; public class AceessOtherAppFileTest extends AndroidTestCase { private static final String TAG = "AceessOtherAppFileTest"; //文件没有发现 public void testAccessOtherAppFile() throws Throwable{ String path = "/data/data/cn.itcast.file/files/www.txt"; File file = new File(path); FileInputStream inStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); Log.i(TAG, new String(data)); } public void testAccessOtherAppReadable() throws Throwable{ String path = "/data/data/cn.itcast.file/files/readable.txt"; File file = new File(path); FileInputStream inStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); Log.i(TAG, new String(data)); } public void testWriteOtherAppReadable() throws Throwable{ String path = "/data/data/cn.itcast.file/files/readable.txt"; File file = new File(path); FileOutputStream outStream = new FileOutputStream(file); outStream.write("xxxx".getBytes()); outStream.close(); } public void testWriteOtherAppWriteable() throws Throwable{ String path = "/data/data/cn.itcast.file/files/writeable.txt"; File file = new File(path); FileOutputStream outStream = new FileOutputStream(file); outStream.write("liming".getBytes()); outStream.close(); } public void testAccessOtherAppWriteable() throws Throwable{ String path = "/data/data/cn.itcast.file/files/writeable.txt"; File file = new File(path); FileInputStream inStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); Log.i(TAG, new String(data)); } public void testWriteOtherAppRW() throws Throwable{ String path = "/data/data/cn.itcast.file/files/rw.txt"; File file = new File(path); FileOutputStream outStream = new FileOutputStream(file); outStream.write("liming".getBytes()); outStream.close(); } public void testAccessOtherAppRW() throws Throwable{ String path = "/data/data/cn.itcast.file/files/rw.txt"; File file = new File(path); FileInputStream inStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); Log.i(TAG, new String(data)); } }
4:MainActivity:SD卡读取
package cn.itcast.file; import cn.itcast.service.FileService; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private FileService fileService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fileService = new FileService(this); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText filenameText = (EditText) findViewById(R.id.filename); EditText contentText = (EditText) findViewById(R.id.filecontent); String filename = filenameText.getText().toString(); String content = contentText.getText().toString(); try { //判断sdcard是否存在于手机上,并且可以进行读写访问 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ fileService.saveToSDCard(filename, content); Toast.makeText(MainActivity.this, R.string.success, 1).show(); }else{ Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show(); } } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, R.string.error, 1).show(); } } }); } }
注意:如果想获得应用程序所在目录下面的file文件夹路径可通过:this.getFilesDir(),若果想获得cache文件夹路可通过: this.getCacheDir().
发表评论
-
Android中AsyncTask的简单用法
2012-01-13 16:00 1122在开发Android移动客户端的时候往往要使用多线程来进行操 ... -
Android应用的自动升级、更新模块的实现 .
2011-11-16 14:01 644http://www.eoeandroid.com/threa ... -
一个APK反编译利器Apktool
2011-11-16 13:54 1553一个APK反编译利器Apktool APK 本地化 ... -
自定义Android标题栏TitleBar布局
2011-11-14 14:13 1214很多网友发现自己Android程序的标题栏TitleBar区域 ... -
Android GPS获取地理位置 .
2011-11-14 14:11 825import android.app.Activity; i ... -
android ListView详解
2011-11-14 13:48 1024在android开发中ListView是比较常用的组件,它以列 ... -
Android之Content provider 详解
2011-11-14 13:35 2435Android是如何实现应用程序之间数据共享的?一个应用程序可 ... -
Android源码地址
2011-11-12 19:14 1022http://blog.csdn.net/ilittleone ... -
Android知识补漏
2011-11-09 22:33 01:AndroidManifiest.xml < ... -
深入剖析Android消息机制
2011-11-09 14:13 934在Android中,线程内部或者线程之间进行信息交互时经常会使 ... -
Android之Handler详解(四)
2011-11-09 14:00 1262d、自己创建新的线程,然后在新线程中创建Looper,主线程调 ... -
Android之Handler详解(三)
2011-11-09 13:58 1347c、将消息队列绑定到子线程上,主线程只管通过Handl ... -
Android之Handler详解(二)
2011-11-09 13:54 1633二:sendMessage版本的Handl ... -
Android之Handler详解(一)
2011-11-09 13:22 2265一个Handler允许你发送和处理消息(Message)以及 ... -
关于StartActivityForResult方法的使用
2011-10-31 17:11 1051根据方法名可知 这个方法是要得到启动后的Activity返回的 ... -
Android 悬浮Activity并可拖动(访悬浮歌词)
2011-10-24 16:23 2047天天动听, 这款Android手机上的音乐播放器,相信不少朋友 ... -
Android GWES
2011-10-24 16:13 1154第八章 Android GWES 8.1 View Syst ... -
Android系统服务-WindowManager
2011-10-24 16:10 1416WindowManager是Android中一个重要的服务 ... -
http通信
2011-10-15 17:31 1067HTTP(HyperText Transfer Proto ... -
android网络与通信(三种网络接口简述 )
2011-10-15 17:27 1413标准Java接口 java.net.*提供与联网有关的类 ...
相关推荐
Android 无法创建File文件 ,在上传图片的时候报错,提示file文件夹未空或在手机清空缓存删除文件夹后 文件夹无法创建 使用file.mkdirs()方法 返回一直是false 提供的功法可以直接解决该问题
Mac查看Android文件(AndroidFileTransfer.dmg) Android File Transfer Browse and transfer files between your Mac computer and your Android device. DOWNLOAD NOW Supports macOS 10.7 and higher. By ...
Android-FileBrowser-FilePicker一个Android文件浏览和选择控件
android webview input=file 失效解决方案
Open AndroidFileTransfer.dmg. Drag Android File Transfer to Applications. Use the USB cable that came with your Android device and connect it to your Mac. Double click Android File Transfer. Browse ...
一行代码完成Android 7 FileProvider适配Demo 通过FileProvider7这个类完成uri的获取即可,例如: FileProvider7.getUriForFile FileProvider7.setIntentDataAndType FileProvider7.setIntentData
Android-android-file-transfer-linux.zip,Android Linux文件传输,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
SelectFile一个android图片选择器
MAC上查看安卓手机利器!
android the file chooser manager this is the file chooser for the android
Android中File类的定义与常用方法.pdf 学习资料 复习资料 教学资源
android 一个好用的filemanager
Android-FileDownloader.zip,多任务、多线程(多连接)、断点恢复、高并发、简单易用、单进程/非单进程,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,...
android系统的 FileExplorer,可以方便管理你手机的文件
Android library for downloading, saving/caching and retrieving any type of files ( image, video, pdf, apk etc ) easily. Download Gradle: repositories { mavenCentral() maven { url '...
Android FileBrowser 文件浏览 界面美观 使用方便
Android中与File有关的权限.pdf 学习资料 复习资料 教学资源
LogToFile ↓支持一下 Android a simple and practical to print to local phone logs Log files open source The log file is written to the tools, the priority SD card -> external memory -> internal ...
androidfiletransfer.dmg 官网无法下载 这里可以哦