How to get selected xls file path from uri for SDK 17 or below for android?
Asked Answered
B

1

0

I need the solution for sdk version 17 or below

this is my method.

    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;
}

i use this method but cursor value null return

help

Biebel answered 28/8, 2017 at 6:33 Comment(3)
look my answer here #45913501Bethannbethanne
Done i need only myuri.getPath(); no need this methodBiebel
you get path at this line "Uri data = result.getData();"Bethannbethanne
V
2

use my FileSelector :

how to use :

 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);
      }
    });

source code : (create class 'FileSelector' and copy/paste)

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;



/**
 * Created by Javad on 2019-11-28 at 4:43 PM.
 */

public class FileSelector {

 private Activity context;
 private String[] extensions;
 private ArrayList<SelectedFile> itemsData = new ArrayList<>();
 public static final String MP4 = ".mp4", MP3 = ".mp3", JPG = ".jpg", JPEG = ".jpeg", PNG = ".png", DOC = ".doc", DOCX = ".docx", XLS = ".xls", XLSX = ".xlsx", PDF = ".pdf";


 public FileSelector(Activity context, String[] extensions) {
 this.context = context;
 this.extensions = extensions;
 }

 public interface OnSelectListener {
 void onSelect(String path);

 }

 public void selectFile(OnSelectListener listener) {
 String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
 listOfFile(new File(sdCard));
 dialogFileList(listener);
 }

 private void listOfFile(File dir) {
 File[] list = dir.listFiles();

 for (File file : list) {
 if (file.isDirectory()) {
 if (!new File(file, ".nomedia").exists() && !file.getName().startsWith(".")) {
 Log.w("LOG", "IS DIR " + file);
 listOfFile(file);
 }
 } else {
 String path = file.getAbsolutePath();

 for (String ext : extensions) {
 if (path.endsWith(ext)) {
 SelectedFile selectedFile = new SelectedFile();

 selectedFile.path = 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");
 }

 private void dialogFileList(OnSelectListener listener) {
 LinearLayout lytMain = new LinearLayout(context);
 lytMain.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
 lytMain.setOrientation(LinearLayout.VERTICAL);
 int p = convertToPixels(12);
 lytMain.setPadding(p, p, p, p);
 lytMain.setGravity(Gravity.CENTER);

 TextView textView = new TextView(context);
 textView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
 textView.setText("JDM File Selecor");

 RecyclerView recyclerView = new RecyclerView(context);

 lytMain.addView(textView);
 lytMain.addView(recyclerView);

 final Dialog dialog = new Dialog(context);
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 // dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
 dialog.setContentView(lytMain);
 dialog.setCancelable(true);
 dialog.show();

 AdapterFile adapter = new AdapterFile(dialog, listener, itemsData);
 recyclerView.setAdapter(adapter);
 LinearLayoutManager LayoutManager = new LinearLayoutManager(context);
 LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
 recyclerView.setLayoutManager(LayoutManager);
 }

 private int convertToPixels(int dp) {
 float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dp * scale + 0.5f);
 }

 private int screenWidth() {
 return context.getResources().getDisplayMetrics().widthPixels;
 }

 private class SelectedFile {
 public String path = "";
 public String name = "";
 }

 private class AdapterFile extends RecyclerView.Adapter<AdapterFile.ViewHolder> {

 private ArrayList<SelectedFile> itemsData;
 private OnSelectListener listener;
 private Dialog dialog;

 public AdapterFile(Dialog dialog, OnSelectListener listener, ArrayList<SelectedFile> itemsData) {
 this.itemsData = itemsData;
 this.listener = listener;
 this.dialog = dialog;
 }

 // Create new views (invoked by the layout manager)
 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

 LinearLayout linearLayout = new LinearLayout(context);
 linearLayout.setOrientation(LinearLayout.VERTICAL);
 TextView txtName = new TextView(context);
 TextView txtPath = new TextView(context);
 txtPath.setTypeface(txtPath.getTypeface(), Typeface.ITALIC);
 txtPath.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);

 linearLayout.addView(txtName);
 linearLayout.addView(txtPath);

 ViewHolder viewHolder = new ViewHolder(linearLayout);
 return viewHolder;
 }

 // inner class to hold a reference to each item of RecyclerView
 public class ViewHolder extends RecyclerView.ViewHolder {

 public LinearLayout linearLayout;
 public TextView txtName;
 public TextView txtPath;

 public ViewHolder(View itemLayoutView) {
 super(itemLayoutView);

 linearLayout = (LinearLayout) itemLayoutView;
 txtName = (TextView) linearLayout.getChildAt(0);
 txtPath = (TextView) linearLayout.getChildAt(1);
 }
 }


 @Override
 public int getItemCount() {
 return itemsData.size();
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, final int position) {
 final SelectedFile selectedFile = itemsData.get(position);

 viewHolder.txtName.setText(selectedFile.name);
 viewHolder.txtPath.setText(selectedFile.path);

 viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 dialog.dismiss();
 listener.onSelect(selectedFile.path);
 }
 });
 }
 }
}

enter image description here

Victorvictoria answered 29/11, 2019 at 12:25 Comment(5)
This will not work on Android 10 (by default) and Android R+ (for all apps), as access to external storage is restricted.Giulietta
Permission RunTime can help : github.com/criss721/Android-Permission-RunTimeVictorvictoria
That will not help on Android 10 or higher.Giulietta
@Giulietta Where I can find the solution to get the absolute path of file selected on Android 10 or higher?Jewry
@SwapnilKale: That is not possible. Not only is there no "absolute path", but you do not have access to the external storage filesystem anyway. Use the Uri, either directly or by copying the content to some file that you control.Giulietta

© 2022 - 2024 — McMap. All rights reserved.