您的位置:首页 > 移动开发 > Android开发

Android从系统图库拿到文件--重命名--通知图库更新

2014-12-01 11:59 393 查看
1、调用系统图库或文件管理器

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] view
plaincopy





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] view
plaincopy





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())));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: