簡體   English   中英

選擇文件時如何從外部SD卡獲取路徑?

[英]how to get path from external sd card while selecting a document?

您好,我是android的新手,我正在制作一個應用程序,可以從SD卡中選擇文檔並獲取該文檔的路徑。 我遵循了教程。 並能夠從內存獲取文件路徑,但是不幸的是我無法從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;
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM