Android从系统图库拿到文件--重命名--通知图库更新
2014-12-01 11:59
393 查看
1、调用系统图库或文件管理器
2、获取用户选取的文件的信息(有路径就有了一切)
3、拿到文件后随便展示,比如放Gridview里,如果是图片。我现在要一个重命名文件的功能
有的时候会出现问题:重命名后会出现0KB的文件,删除以后,文件管理器中没有该文件,但是图库中还有该文件,但是图片大小为0
======================================
另刷新相册:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + file)));
[java] view
plaincopy
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
但在Android4.4中,则会抛出以下异常:
[java] view
plaincopy
MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + fileName}, null, null);
--------
Intent intentFromGallery = new Intent(); // intentFromGallery.setType("image/*,audio/*,video/*"); // 设置文件类型 intentFromGallery.setType("*/*"); intentFromGallery.addCategory(Intent.CATEGORY_OPENABLE); intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); // intentFromGallery.setAction(android.content.Intent.ACTION_VIEW); // File file = new File("/sdcard"); // intentFromGallery.setDataAndType(Uri.fromFile(file), "image/*,audio/*,video/*"); startActivityForResult(intentFromGallery, 1);
2、获取用户选取的文件的信息(有路径就有了一切)
File tempf = null; try { Uri uri = data.getData();//content://media/external/images/media/74984 String[] proj = { MediaStore.Images.Media.DATA }; //_data Cursor actualimagecursor = this.managedQuery(uri,proj,null,null,null); if(actualimagecursor == null){ ToastUtils.showToast(getApplicationContext(), "不支持该类型"); return; } int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index); //字段索引 tempf = new File(img_path); } catch (Exception e) { e.printStackTrace(); }
3、拿到文件后随便展示,比如放Gridview里,如果是图片。我现在要一个重命名文件的功能
String filePathString=uploadFileBeanListTemp_imgs.get(position).getSdCardPath(); File oldFile = new File(filePathString); String oldName = oldFile.getName(); String newPath = oldFile.getParent() +File.separator + newName + oldName.substring(oldName.lastIndexOf(".")); // boolean result = FileUtils.renameByCopy(filePathString, newPath); boolean result = oldFile.renameTo(new File(newPath)); if(result){ //oldFile.delete(); Intent intentn = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File newFile = new File(newPath); intentn.setData(Uri.fromFile(newFile)); sendBroadcast(intentn); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(oldFile)); sendBroadcast(intent);
有的时候会出现问题:重命名后会出现0KB的文件,删除以后,文件管理器中没有该文件,但是图库中还有该文件,但是图片大小为0
======================================
另刷新相册:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + file)));
通常在我们的项目中,可能会遇到写本地文件,最常用的就是图片文件,在这之后需要通知系统重新扫描SD卡,
在Android4.4之前也就是以发送一个Action为“Intent.ACTION_MEDIA_MOUNTED”的广播通知执行扫描。如下:
[java] viewplaincopy
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
但在Android4.4中,则会抛出以下异常:
W/ActivityManager( 498): Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2269, uid=20016
那是因为Android4.4中限制了系统应用才有权限使用广播通知系统扫描SD卡。
解决方式:
使用MediaScannerConnection执行具体文件或文件夹进行扫描。
[java] viewplaincopy
MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + fileName}, null, null);
--------
// // 其次把文件插入到系统图库 // try { // MediaStore.Images.Media.insertImage(getContentResolver(),newFile.getPath(), newFile.getName(), null); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // // 最后通知图库更新 file:////storage/emulated/0/Screenshots/nshot_2014-10-13-10-10-40.jpg // sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file:///" + newFile.getPath())));
相关文章推荐
- 主动通知Android系统图库进行更新
- android通知扫描某个文件或者文件夹(图库及时更新图片)
- 主动通知Android系统图库进行更新
- 主动通知Android系统图库进行更新
- 主动通知Android系统图库进行更新
- 主动通知Android系统图库进行更新
- 主动通知Android系统图库进行更新(图片保存到手机中,立即在图库中显示1)
- 主动通知Android系统图库进行更新
- Android 文件断点下载和通知栏的提示及apk更新安装
- Android 查询出手机系统图库和拍照保存后的广播更新
- 搭建android版本更新服务器使用android系统自带的DownloadManager下载文件
- TFTP更新linux或android系统文件
- 通知Android系统相册更新
- Android系统源码阅读(9):Content Provider数据更新通知
- Android保存图片到系统图库并通知系统相册刷新
- Android O 8.0系统下通知(Notification)、安装apk问题更新后的简单兼容写法
- Android-更新系统图库碰到的问题
- android中通知系统扫描系统媒体文件的几种方法
- Android通知图库更新
- android 把文件插入到系统图库