最近在做一个类似文件管理器,app是采用本地数据库模拟目录的方式,管理与其手机储存对应的存储文件。那么需求来了,需要在app中添加一张从本地相册中选出来的照片,并将照片复制到自己app指定的目录下。这个是一个文件复制的过程,在复制过程中需要实时的将文件复制的进度通过一个dialog来显示出来,并且也将复制后的路径保存到了安卓的手机数据库中。
先看效果图:

我们加工的材料: 使用本地相机的意图来打开相册,并在OnActivityResult中获取到所选相册的路径。 使用FileInputStream 和FileOutputStream 文件的输入流和输出流对相册的文件进行解析,保存到SD卡中。 通过使用Handler 将复制的信息发送给ProgressDialog 展示进度。 好啦大概知道这么几个工具,接下来就看看代码。

一、调用系统相机返回所选照片文件的路径

打开相机

打开系统相机,最方便快捷的要数使用Intent意图来启动。 注意一点如果你API版本高于24那么你需要动态申请权限,并且要使用 FileProvider ,大家可以查一下如何使用。

 private void showAlbumAction(){bottomDialog.dismiss();String imgName = "IMG-"+ DateUtils.getStringDate();alubmPhoPath = currentFilePath+imgName+ IFileType.FILE_TYPE_JPG;File mGalleryFile = new File(alubmPhoPath) ;Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果大于等于7.0使用Uri uriForFile= FileProvider.getUriForFile(getActivity(),getActivity().getPackageName() + ".provider", mGalleryFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);startActivityForResult(intent, IEventType.EVENT_REQUEST_SELECT_PIC_KITKAT_ALBUM); }else { startActivityForResult(intent, IEventType.EVENT_REQUEST_ALBUM);}}

OnAtivityResult接收相册回调方法

    @SuppressLint("WrongConstant")@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);Toast.makeText(getActivity(), "fragemntaaaaawwwww", 0).show();if (resultCode==RESULT_OK) {switch (requestCode) {case IEventType.EVENT_REQUEST_ALBUM:final File imgFile = new File(GetImgPath.getPath(getContext(), data.getData()));//拿到照片的路径构造成File类型的文件Log.i("imgUri",imgFile.getAbsolutePath()+"") ;filePresenter.copyFile(imgFile.getPath(),alubmPhoPath); //文件copy的工具类,我下边将贴出方法break;case default:break;}}}

其中GetImagePath工具类是国外的一个大神写的,主要是兼容24之前和24之后拿到文件路径的一个类,我贴到下边:

package com.shao.jobsnaps.utils;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;public  class GetImgPath {// 4.4以上 content://com.android.providers.media.documents/document/image:3952 // 4.4以下 content://media/external/images/media/3951public static String getPath(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProviderif (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {if (isExternalStorageDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}}// DownloadsProviderelse if (isDownloadsDocument(uri)) {final String id = DocumentsContract.getDocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));return getDataColumn(context, contentUri, null, null);} // MediaProviderelse if (isMediaDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);} else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address iif (isGooglePhotosUri(uri)) return uri.getLastPathSegment();return getDataColumn(context, uri, null, null);} // Fileelse if ("file".equalsIgnoreCase(uri.getScheme())) {return uri.getPath();}}return null;}//Android 4.4以下版本自动使用该方法public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);if (cursor != null && cursor.moveToFirst()) {final int index = cursor.getColumnIndexOrThrow(column);return cursor.getString(index);}} finally {if (cursor != null) cursor.close();}return null;}/*** @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider.*/public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider.*/public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider.*/public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check. * @return Whether the Uri authority is Google Photos.*/public static boolean isGooglePhotosUri(Uri uri) {return "com.google.android.apps.photos.content".equals(uri.getAuthority());}
}

二、FileUtils文件拷贝copyFiles方法

FileUtils 文件拷贝中有N 个方法, 那么我只贴这个copyFiles方法 我们在封装的时候记得一定要把android.os.Handler作为参数传进去,通过Handler 不停的向我们的主线程的 handleMessage发送拷贝进度的一个值,handleMessage 接收到以后就能调用ProgressDialog显示进度了。
copyFiles方法如下:

拷贝文件并发送消息

 /***  复制单个文件 * @param oldPath 原文件路径 如:c:/fqf.txt String* @param newPath 复制后路径 如:f:/fqf.txt*/public static void copyFile(String oldPath, String newPath, Handler handler) {try {int bytesum = 0;int byteread = 0;File oldfile = new File(oldPath);if (oldfile.exists()) {//文件存在时 InputStream inStream = new FileInputStream(oldPath);//读入原文件 FileOutputStream fs = new FileOutputStream(newPath);byte[] buffer = new byte[1444];int length;int value = 0 ;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;//字节数 文件大小 value ++ ;  //计数System.out.println("完成"+bytesum+"  总大小"+fileTotalLength);fs.write(buffer, 0, byteread);Message msg  = new Message(); //创建一个msg对象msg.what =110 ;  msg.arg1 = value ; //当前的valuehandler.sendMessage(msg) ;Thread.sleep(10);//每隔10ms发送一消息,也就是说每隔10ms value就自增一次,将这个value发送给主线程处理}inStream.close();}} catch (Exception e) {System.out.println("复制单个文件操作出错");e.printStackTrace();}}

接收消息

有了copy发送出来的消息,那么就有接收消息的,接下来在你的
Activity 或者你的Fragment 或者 你的 Presenter 中写一个 Handler 重写的HanldeMessage方法接收消息。
如下:

 Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 110:Log.i("我收到消息",""+msg.arg1) ;//拷贝过程中更新进度条updateProgressDialog(msg.arg1);//msg.arg1其实就是copyFile发过来的value值if(msg.arg1==100) {//拷贝完了就隐藏进度条hideProgressDialog();showMsg("转储完成!");}break;}}} ;

ProgressDialog

ProgressDIalog 是 android.app 包下的类,直接拿来用就行了。
由上边可以知道,我们分开写这么几个方法来分别管理 创建、更新进度条、关闭进度条的方法,由此可以很容易理解一下给出自己写的这三个方法:

创建dialog进度条

  public void showProgressDialog(String titile) {progressDialog = new ProgressDialog(getActivity()) ;progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) ; //设置为水平进度条,当然你也可以设置为圆形progressDialog.setCancelable(false); //通过点击back是否可以退出dialogprogressDialog.setCanceledOnTouchOutside(false); //通过点击外边是否可以dissprogressDialog.setIcon(R.drawable.ic_more_horiz_black_24dp);progressDialog.setTitle(titile);//设置标题progressDialog.setMax(100);//设置到头的数值,这里就用100结束了progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { //你懂的的点击方法@Overridepublic void onClick(DialogInterface dialog, int which) {}});progressDialog.show();  //不能忘的最后一句show ,没有他上边再怎么写也是显示不出来滴!}

更新进度方法

//更新进度方法
public void updateProgressDialog(int value){if(progressDialog!=null) {progressDialog.setProgress(value);}}

关闭progressDilog

    @Overridepublic void hideProgressDialog() {if(progressDialog!=null)progressDialog.dismiss();}
    值得注意的是:以上写法 progressDialog 记得声明为全局变量

再项目开发中,有可能会用到mvp模式, 你把这几个方法拆开写, 把接收消息的写再 Presenter中, 去调用 dialog的开关。就可以了。


版权声明
author :shaoduo
原文来自:http://blog.csdn.net/shaoduo/article/details/77009794
其他出处均为转载,原创作品,欢迎读者批评指正。

android调用系统相册将图片复制到指定目录下并显示复制进度相关推荐

  1. android+代码调用+相册+小米,Android调用系统相册选择图片,支持小米4云相册

    用小米4调用系统相册选择照片时,如果云相册功能开启的话.云相册中的图片也会显示在选择列表中.经过测试,选择到云相册中的图片的话,uri的scheme是file,而不再试content.本文支持云相册的 ...

  2. android 调用相册功能吗,Android调用系统相册选择图片,支持小米4云相册

    用小米4调用系统相册选择照片时,如果云相册功能开启的话.云相册中的图片也会显示在选择列表中.经过测试,选择到云相册中的图片的话,uri的scheme是file,而不再试content.本文支持云相册的 ...

  3. 学习笔记之Android调用系统相册选择图片

    文章目录 前言 使用步骤 小结 参考资料 前言 在相册里选择图片上传也是很常见的功能了例如微信朋友圈等等.但是他们是自定义的选择器,可以选择多张图片并修改.这里我们讲一个最简单的:调用系统的相册选择一 ...

  4. android调用系统相册打开图片不显示,【报Bug】打开相册,不显示图片,选中图片后,app会崩溃...

    产品分类: uniapp/App PC开发环境操作系统: Mac PC开发环境操作系统版本号: 10 HBuilderX类型: 正式 HBuilderX版本号: 2.8.8 手机系统: Android ...

  5. Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto)void onclick() {Intent intent = new Intent(MediaSt ...

  6. android固定位置拍照,Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto) void onclick() { Intent intent = new Intent(Media ...

  7. Android调用系统相册、拍照以及裁剪最简单的实现(兼容7.0)

    这里我只实现功能,具体Android 7.0 的一些细节参考 http://blog.csdn.net/lmj623565791/article/details/72859156 具体步骤: 一.在清 ...

  8. Android 7.0拍照后保存到手机的指定目录并返回显示

    欢迎访问本人博客:https://www.kangaroohy.com 本文参考第一行代码Android第2版.实现拍照并保存到指定目录. 1.创建项目Camera,修改activity_main.x ...

  9. AndroidStudio调用系统相册选择图片并显示、将图片保存到私有空间

    有时候为了能让用户自定义软件的背景,我们需要实现从系统相册选择一张图片并将其进行保存,在后面打开的时候继续使用该图片充当背景.为什么要保存到私有空间呢?保存和到读取私有空间的内容是不需要权限的.由于实 ...

最新文章

  1. sql-schema与catalog
  2. 北大教授杨超、清华教授唐杰获首届“王选杰出青年学者奖”
  3. MSM8225 thermal设计
  4. 安卓逆向工具apkide安装
  5. LayUi引入JSON文件进行表格重载时出现接口请求异常
  6. sigprocmask 阻塞进程
  7. hibernate的数据关联映射
  8. Maven教程:tutorialspoint-maven
  9. android chrome 无法下载,Android Chrome浏览器将支持下载暂停和取消
  10. python入门自学软件手机版_python编程入门
  11. Python下载qq音乐歌曲实例教程
  12. JavaScript 实例:点击漫天小星星 (获取整个浏览器窗口的宽高)
  13. wpa_cli的使用
  14. iPhone卫星功能仅用于紧急通信;韩国通过立法禁止苹果、谷歌垄断支付系统;Linux 5.14 版本发布|极客头条
  15. c语言字母字符参与运算,c语言字符串可以参加运算吗?
  16. 修改文件与文件提交--乐字节Java
  17. 快速查找计算9百万万亿整数内全部素数(质数)的C++代码
  18. 自动驾驶 9-1: (线性)卡尔曼滤波器The (Linear) Kalman Filter
  19. Windows Server HP 服务器磁盘阵列检测
  20. trixbox不相信眼泪

热门文章

  1. HOW TO:菜单名称有关操作
  2. 基于springboot+vue框架的二手书交易网站
  3. Chapter15 Principles of Metabolic Regulation
  4. pdfbox 去除签名 保留签名图片
  5. web前端学习笔记——选择器
  6. 浏览器是怎样解析 CSS 选择器的?
  7. 安装oracle-- redhat-- ins_ctx.mk问题
  8. 联想linux改windows,G40-70、G50-70联想小新笔记本SR1000随机Linux改Windows 7系统操作指导...
  9. php打印99乘法表加粗,PHP基础循环语句之打印99乘法表
  10. ABF平台设计(五)-物料中心/脚手架