Java 如何从android SDK 17或更低版本的uri中获取选定的xls文件路径?
我需要sdk版本17或更低版本的解决方案 这是我的方法Java 如何从android SDK 17或更低版本的uri中获取选定的xls文件路径?,java,android,getfiles,Java,Android,Getfiles,我需要sdk版本17或更低版本的解决方案 这是我的方法 public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader(
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
我使用这个方法,但游标值返回null
helppath=uri.getPath();只有此行用于获取所选文件的路径
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/vnd.ms-excel");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(getApplicationContext(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
// Get the path
try {
path = uri.getPath();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
path=uri.getPath();只有此行用于获取所选文件的路径
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/vnd.ms-excel");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(getApplicationContext(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
// Get the path
try {
path = uri.getPath();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
使用我的文件选择器:
new FileSelector(this, new String[]{FileSelector.XLS, FileSelector.XLSX}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
new FileSelector(this, new String[]{".jpg", ".jpeg"}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
如何使用:
new FileSelector(this, new String[]{FileSelector.XLS, FileSelector.XLSX}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
new FileSelector(this, new String[]{".jpg", ".jpeg"}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
源代码:(创建类“FileSelector”并复制/粘贴)
导入android.app.Activity;
导入android.app.Dialog;
导入android.graphics.Typeface;
导入android.os.Environment;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.util.TypedValue;
导入android.view.Gravity;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.Window;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入java.io.File;
导入java.util.ArrayList;
/**
*Javad于2019-11-28下午4:43创建。
*/
公共类文件选择器{
私人活动语境;
私有字符串[]扩展名;
private ArrayList itemsData=new ArrayList();
公共静态最终字符串MP4=“.MP4”、MP3=“.MP3”、JPG=“.JPG”、JPEG=“.JPEG”、PNG=“.PNG”、DOC=“.DOC”、DOCX=“.DOCX”、XLS=“.XLS”、XLSX=“.XLSX”、PDF=“.PDF”;
公共文件选择器(活动上下文,字符串[]扩展名){
this.context=上下文;
this.extensions=扩展;
}
公共接口OnSelectListener{
void onSelect(字符串路径);
}
public void selectFile(OnSelectListener侦听器){
字符串sdCard=Environment.getExternalStorageDirectory().getAbsolutePath();
列表文件(新文件(SD卡));
dialogFileList(侦听器);
}
私有无效列表文件(文件目录){
File[]list=dir.listFiles();
用于(文件:列表){
if(file.isDirectory()){
如果(!new File(File,“.nomedia”).exists()&&!File.getName().startsWith(“.”){
w(“Log”,“IS DIR”+文件);
列表文件(文件);
}
}否则{
字符串路径=file.getAbsolutePath();
for(字符串扩展名:扩展名){
if(路径endsWith(ext)){
SelectedFile SelectedFile=新建SelectedFile();
selectedFile.path=路径;
String[]split=path.split(“/”);
selectedFile.name=split[split.length-1];
itemsData.add(selectedFile);
Log.i(“Log”、“ADD”+selectedFile.path+“”+selectedFile.name);
}
}
}
}
Log.d(“Log”,itemsData.size()+“DONE”);
}
私有无效对话框文件列表(OnSelectListener侦听器){
LinearLayout lytMain=新的LinearLayout(上下文);
lytMain.setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
lytMain.设置方向(线性布局.垂直);
int p=转换像素(12);
lytMain.setPadding(p,p,p,p);
lytMain.setGravity(重心);
TextView TextView=新的TextView(上下文);
textView.setLayoutParams(新的LinearLayout.LayoutParams(screenWidth(),ViewGroup.LayoutParams.WRAP_内容));
setText(“JDM文件Selecor”);
RecyclerView RecyclerView=新的RecyclerView(上下文);
lytMain.addView(textView);
lytMain.addView(recyclerView);
最终对话框=新对话框(上下文);
对话框.requestWindowFeature(窗口.FEATURE\u无\u标题);
//dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(lytMain);
对话框。可设置可取消(true);
dialog.show();
AdapterFile adapter=新适配器文件(对话框、侦听器、项数据);
recyclerView.setAdapter(适配器);
LinearLayoutManager LayoutManager=新的LinearLayoutManager(上下文);
LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(LayoutManager);
}
私有int-convertToPixels(int-dp){
浮动比例=context.getResources().getDisplayMetrics().density;
返回(int)(dp*刻度+0.5f);
}
私有int屏幕宽度(){
返回context.getResources().getDisplayMetrics().widthPixels;
}
私有类SelectedFile{
公共字符串路径=”;
公共字符串名称=”;
}
私有类适配器文件扩展了RecyclerView。适配器使用my文件选择器:
new FileSelector(this, new String[]{FileSelector.XLS, FileSelector.XLSX}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
new FileSelector(this, new String[]{".jpg", ".jpeg"}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
如何使用:
new FileSelector(this, new String[]{FileSelector.XLS, FileSelector.XLSX}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
new FileSelector(this, new String[]{".jpg", ".jpeg"}).selectFile(new FileSelector.OnSelectListener() {
@Override
public void onSelect(String path) {
G.toast(path);
}
});
源代码:(创建类“FileSelector”并复制/粘贴)
导入android.app.Activity;
导入android.app.Dialog;
导入android.graphics.Typeface;
导入android.os.Environment;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.util.TypedValue;
导入android.view.Gravity;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.Window;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入java.io.File;
导入java.util.ArrayList;
/**
*Javad于2019-11-28下午4:43创建。
*/
公共类文件选择器{
私人活动语境;
私有字符串[]扩展名;
private ArrayList itemsData=new ArrayList();
公共静态最终字符串MP4=“.MP4”、MP3=“.MP3”、JPG=“.JPG”、JPEG=“.JPEG”、PNG=“.PNG”、DOC=“.DOC”、DOCX=“.DOCX”、XLS=“.XLS”、XLSX=“.XLSX”、PDF=“.PDF”;
公共文件选择器(活动上下文,字符串[]扩展名){
this.context=上下文;
this.extensions=扩展;
}
公共接口OnSelectListener{
void onSelect(字符串路径);
}
public void selectFile(OnSelectListener侦听器){
字符串sdCard=Environment.getExternalStorageDirectory().getAbsolutePath();
列表文件(新文件(SD卡));
dialogFileList(侦听器);
}
私有无效列表文件(文件目录){
File[]list=dir.listFiles();
用于(文件:列表){
if(file.isDirectory()){
如果(!new File(File,“.nomedia”).exists()&&!File.getName().startsWith(“.”){
w(“Log”,“IS DIR”+文件);
列表文件(文件);
}
}否则{
字符串路径=file.getAbsolutePath();
for(字符串扩展名:扩展名){
if(路径endsWith(ext)){
SelectedFile SelectedFile=新建SelectedFile();
选定的文件