在外存存储图片的一个典型示例
2012-08-03 11:34
387 查看
void createExternalStoragePublicPicture() { // Create a path where we will place our picture in the user's // public pictures directory. Note that you should be careful about // what you place here, since the user often manages these files. For // pictures and other media owned by the application, consider // Context.getExternalMediaDir(). File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File file = new File(path, "DemoPicture.jpg"); try { // Make sure the Pictures directory exists. path.mkdirs(); // Very simple code to copy a picture from the application's // resource into the external file. Note that this code does // no error checking, and assumes the picture is small (does not // try to copy it in chunks). Note that if external storage is // not currently mounted this will silently fail. InputStream is = getResources().openRawResource(R.drawable.balloons); OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); // Tell the media scanner about the new file so that it is // immediately available to the user. MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } }); } catch (IOException e) { // Unable to create file, likely because external storage is // not currently mounted. Log.w("ExternalStorage", "Error writing " + file, e); } } void deleteExternalStoragePublicPicture() { // Create a path where we will place our picture in the user's // public pictures directory and delete the file. If external // storage is not currently mounted this will fail. File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File file = new File(path, "DemoPicture.jpg"); file.delete(); } boolean hasExternalStoragePublicPicture() { // Create a path where we will place our picture in the user's // public pictures directory and check if the file exists. If // external storage is not currently mounted this will think the // picture doesn't exist. File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File file = new File(path, "DemoPicture.jpg"); return file.exists(); }原文链接:http://developer.android.com/reference/android/os/Environment.html
相关文章推荐
- 简单的一个远传采集并下载远传图片存储到本地示例
- [笔记]python网络爬虫:一个简单的爬取图片并存储示例
- 一个图片数据的存和取的示例
- 一个使用boost::thread_specific_ptr线程本地存储的示例,value将不受其它线程影响
- 一个产生随机图片验证码的示例
- 一个分页的类,分页的存储过程,分页的jsp示例
- 手机网页示例2——一个简单的顶上bar图片切换效果
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序
- 一个伪ajax图片上传代码实现示例
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序
- 一个典型的SWIG源码示例文件
- 一个产生随机图片验证码的示例
- 一个图片显示的JS示例
- 转 一个在HTML 中播放Flash 图片的示例
- python中初始化一个数组用来存储多张图片
- mysql blob字段存储图片操作示例
- 创建一个输出参数为xml类型的存储过程(示例)
- 一个最简单的php上传图片示例
- VC++6.0 一个简单的图片按钮示例
- 一个伪ajax图片上传代码实现示例