【问题标题】:how to get path from external sd card while selecting a document?选择文档时如何从外部SD卡获取路径?
【发布时间】:2016-05-30 10:53:46
【问题描述】:

您好,我是 android 新手,我正在制作一个应用程序,我可以在其中从 sd 卡中选择文档并获取该文档的路径。我跟着this 教程。并且能够从内部存储器获取文件路径,但不幸的是我无法从 sd 卡获取路径。

活动

public class Buttona extends Activity {
    private static final int MY_INTENT_CLICK=302;
    String selectedFilePath;
    TextView texta;
    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buttona);
    }

    // Start the service
    public void startService(View view) {

            if (Build.VERSION.SDK_INT < 19) {
                Intent intent = new Intent();
                intent.setType("*/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select File"), MY_INTENT_CLICK);
            } else {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                startActivityForResult(intent, MY_INTENT_CLICK);
            }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK)
        {
            if (requestCode == MY_INTENT_CLICK)
            {
                try {
                    if (data != null) {

                        Uri uri = data.getData();

                        String filePath = getPath(mContext, uri);
                        String fileName = getFileName(data, mContext);



                        Log.e("TAG", filePath);
                        Log.e("TAG", fileName);




                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getFileName(Intent data,Context context){

        Uri uri = data.getData();
        String uriString = uri.toString();
        File myFile = new File(uriString);
        String path = myFile.getAbsolutePath();
        String displayName = null;

        if (uriString.startsWith("content://")) {
            Cursor cursor = null;
            try {
                cursor =context. getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                }
            } finally {
                cursor.close();
            }
        } else if (uriString.startsWith("file://")) {
            displayName = myFile.getName();
        }


        return  displayName;
    }

//get file path

    public static String getPath(Context context, Uri uri) throws URISyntaxException {
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        }
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    // Stop the service
    public void stopService(View view) {
        stopService(new Intent(this, MyService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

【问题讨论】:

标签: android android-activity android-file android-external-storage


【解决方案1】:
         try {
                if (data != null) {

                    Uri uri = data.getData();

                    String filePath = getPath(mContext, uri);
                    String fileName = getFileName(data, mContext);



                    Log.e("TAG", filePath);
                    Log.e("TAG", fileName);




                }
            } catch (Exception e) {
                e.printStackTrace();
            }

获取文件路径和文件名

   //get file name
public static String getFileName(Intent data,Context context){

    Uri uri = data.getData();
    String uriString = uri.toString();
    File myFile = new File(uriString);
    String path = myFile.getAbsolutePath();
    String displayName = null;

    if (uriString.startsWith("content://")) {
        Cursor cursor = null;
        try {
            cursor =context. getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    } else if (uriString.startsWith("file://")) {
        displayName = myFile.getName();
    }


    return  displayName;
}

//get file path

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

【讨论】:

  • 什么是 myFile.getAbsolutePath() && myFile.getName() ?
  • @AnshulKhare getPath 和 getFileName 是两个静态方法,如果您想获取文件路径而不是仅使用 getPath() 方法,在 onActivityResult 中只需传递参数,作为响应,您将获得您选择的 filePath
  • 我收到错误,无法解析 getAbsolutePath();并且无法解析 getName();
  • @AnshulKhare 你在哪里得到错误?我试试这个,它对我来说非常有用,我没有收到任何错误
  • 字符串路径 = myFile.getAbsolutePath(); & displayName = myFile.getName();在这两个问题上,我都遇到了无法解决方法的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
相关资源
最近更新 更多