簡體   English   中英

Android,Xamarin:獲取SD卡的文件路徑

[英]Android, Xamarin: Get File Path Of SD Card

我目前正在開發一個應用程序,它會通過您的手機並列出所有可用的 MP3 文件。 我設法完成了這項工作並搜索了內部存儲上的所有內容,但沒有設法找到一種使用環境來訪問 sd 卡的方法,當安裝一個時。 這是我的代碼 - 當 SD 卡為 TRUE 時,你會看到缺失的部分。 你能完成嗎?

    public List<string> ReturnPlayableMp3(bool sdCard)
    {
        List<string> res = new List<string>();
        string phyle;

        if(sdCard)
        {
            // missing 
        }
        else
        {
            try
            {
                var path1 = Android.OS.Environment.ExternalStorageDirectory.ToString();
                var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);

                foreach (string currentFile in mp3Files)
                {
                    phyle = currentFile;
                    res.Add(phyle);
                }
            }
            catch (Exception e9)
            {
                Toast.MakeText(ApplicationContext, "ut oh\n" + e9.Message, ToastLength.Long).Show();
            }
        }


        return res;
    }


}

它需要返回與內部存儲完全相同的內容,只是這次針對 sd 卡。 現在,返回的是:

""/存儲/模擬/0""

我希望你能幫助我。 謝謝!

所以我找到了它的位置:/storage/05B6-2226/

但數字僅指我的 SD 卡。 如何以編程方式獲取此路徑?

看一下這些方法:

返回應用程序可以放置其擁有的持久文件的主外部文件系統(在Environment.ExternalStorageDirectory上的某個位置)上目錄的絕對路徑。 這些文件是應用程序的內部文件,通常作為介質對用戶不可見。

將絕對路徑返回到應用程序可以放置其擁有的持久文件的所有外部存儲設備上的應用程序特定目錄的絕對路徑。 這些文件是應用程序的內部文件,通常作為介質對用戶不可見。

我一直在尋找幾天的解決方案,這些解決方案最終為您提供了“外部”內置存儲。 最終找到了針對“可移動” SD卡的解決方案,並希望將其發布在此處,以防其他人仍在尋找。

如何在Xamarin.android中的mashmallow中寫入外部存儲SD卡

//Get the list of External Storage Volumes (E.g. SD Card) 
Context context = Android.App.Application.Context;
var storageManager = (Android.OS.Storage.StorageManager)context.GetSystemService(Context.StorageService);
var volumeList = (Java.Lang.Object[])storageManager.Class.GetDeclaredMethod("getVolumeList").Invoke(storageManager);

List<Java.IO.File> ExtFolders = new List<Java.IO.File>();

//Select the Directories that are not Emulated
foreach (var storage in volumeList)
{

    Java.IO.File info = (Java.IO.File)storage.Class.GetDeclaredMethod("getDirectory").Invoke(storage);
    if ((bool)storage.Class.GetDeclaredMethod("isEmulated").Invoke(storage) == false && info.TotalSpace > 0)
    {
        //Get Directory Path
        Console.WriteLine(info.Path);
    }
}

只是想分享我的答案,我在哪里獲得了 extStorages 路徑,我在我的簡單文件瀏覽器應用程序中使用了這個方法。

public static string[] GetRemovableStorages()
{
    List<string> extStorage = new List<string>();
    //If this throws exception
    string storageDir = (string)Environment.StorageDirectory;
    //Try this
    string storageDir = Directory.GetParent (Environment.ExternalStoragePublicDirectory).Parent.FullName;
    string[] directories = Directory.GetDirectories(storageDir);
        
    foreach(string dir in directories)
    {
        try
        {
            var extStoragePath = new Java.IO.File(dir);
            bool isRemovable = Environment.InvokeIsExternalStorageRemovable(extStoragePath);
                
            if(isRemovable) extStorage.Add(extStoragePath.AbsolutePath);
            else return null;
        }   
        catch
        {
        
        }
    }
        
    return extStorage.ToArray();
}

Elikill58 的回答在我的情況下拋出異常沒有這樣的方法“getDirectory”,但我推薦 Elikill58 的回答

暫無
暫無

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

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