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

来源:互联网 发布:数据流程图符号含义 编辑:程序博客网 时间:2024/05/01 20:12

最近在做一个类似文件管理器,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")    @Override    public 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/3951    public static String getPath(final Context context, final Uri uri) {        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider        if (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];                }            }// DownloadsProvider            else 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);            } // MediaProvider            else 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 i                if (isGooglePhotosUri(uri)) return uri.getLastPathSegment();                return getDataColumn(context, uri, null, null);            } // File            else 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 ; //当前的value                    handler.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(){            @Override            public 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是否可以退出dialog        progressDialog.setCanceledOnTouchOutside(false); //通过点击外边是否可以diss        progressDialog.setIcon(R.drawable.ic_more_horiz_black_24dp);        progressDialog.setTitle(titile);//设置标题        progressDialog.setMax(100);//设置到头的数值,这里就用100结束了        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { //你懂的的点击方法            @Override            public void onClick(DialogInterface dialog, int which) {            }        });        progressDialog.show();  //不能忘的最后一句show ,没有他上边再怎么写也是显示不出来滴!    }

更新进度方法

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

关闭progressDilog

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

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


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

阅读全文
0 0