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
=================&