我将此代码用于视频
public void pickVideo()
{
Intent pickVideoIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoIntent, PICK_VIDEO_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK){
switch (requestCode){
case PICK_VIDEO_ACTIVITY_REQUEST_CODE:
Uri selectedVideo = data.getData();
videoPicked(videoUriToRealPath(selectedVideo));
break;
.
.
.
public String videoUriToRealPath(Uri videoUri){
String[] proj = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(videoUri, proj, null, null, null);
String videoPath = "";
try{
if (cursor != null){
int column_index = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
if (cursor.moveToFirst()){
videoPath = cursor.getString(column_index);
}
}
}finally{
cursor.close();
}
return videoPath;
}
它正在工作。我想将 PICK_FILE_ACTIVITY_REQUEST_CODE 设置为一个案例。我已经定义了它:
public void pickFile()
{
Intent pickFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickFileIntent.setType("*/*");
startActivityForResult(pickFileIntent, PICK_FILE_ACTIVITY_REQUEST_CODE );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK){
switch (requestCode){
case PICK_FILE_ACTIVITY_REQUEST_CODE:
Uri selectedFile = data.getData();
filePicked(fileUriToRealPath(selectedFile));
.
.
.
public String fileUriToRealPath(Uri fileUri){
String[] proj = {MediaStore.Files.Media.DATA};
Cursor cursor = getContentResolver().query(fileUri, proj, null, null, null);
String filePath = "";
try{
if (cursor != null){
int column_index = cursor.getColumnIndex(MediaStore.Files.Media.DATA);
if (cursor.moveToFirst()){
filePath = cursor.getString(column_index);
}
}
}finally{
cursor.close();
}
return filePath;
}
实际上它没有用。在我的文件中扩展.PDF和.DOCX。如何解决?
答案 0 :(得分:0)
我认为你需要搜索整个SD卡。
<强> searchDir(Environment.getExternalStorageDirectory()); 强>
public void searchDir(File dir){
String docExt = ".doc";
String pdfExt = ".pdf";
File listFile[] = dir.listFiles();
if (listFile != null){
for (int i = 0; i < listFile.length; i++){
if (listFile[i].isDirectory()){
searchDir(listFile[i]);
}else{
if(listFile[i].getName().endsWith(docExt)){
//Do what ever u want
}else if(listFile[i].getName().endsWith(pdfExt)){
//Do what ever u want
}
}
}
}
}