Android文件路径(Xamarin) [英] Android File Path (Xamarin)

查看:1406
本文介绍了Android文件路径(Xamarin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在选择与意图的文件。我的问题是,返回的路径是不正确的格式。

I'm currently trying to select a file with an Intent. My Problem is, that the path returned is not in the right format.

我的意图:

private void selectAudioFile(object sender, EventArgs eventArgs)
{
    Intent = new Intent();
    Intent.SetType("audio/*");
    Intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(Intent, "Select Audio File"), PickAudioId);
}

而rusult方式:

And the rusult method:

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
    base.OnActivityResult (requestCode, resultCode, data);
    if ((resultCode == Result.Ok) && (requestCode == PickAudioId) && (data != null)) {
        Android.Net.Uri uri = data.Data;
        if (!File.Exists(uri)) {
            // error
        }
    }
}

问题:

我需要处理的的文件的类所接收到的路径。路径看起来像的 /文件/音频:1234 的。 如果我请与 File.Exists(URI)的路径的最高审计机关是该文件不存在。

I need to handle the received path with the File class. The path looks like /document/audio:1234. If I check the path with File.Exists(uri) it sais that the file does not exist.

我怎样才能获得路径的格式我可以处理的文件的这个文件,比如 /storage/emulated/0/Music/foo.mp3 的或类似的东西是什么?

How can i get the path to this file in a format i can handle with File like /storage/emulated/0/Music/foo.mp3 or something like that ?

感谢您的帮助

推荐答案

你得到的回报可能看起来像一个路径,但它不是URI,它更像是一个数据库记录的快捷方式。

The uri you get in return may look like a path but it's not, it's more like a shortcut to a database record.

要找回真实的路径是太拗口了,你必须使用一个ContentResolver的,这里是如何找回它(基于的从Xamarin 的样品):

To retrieve a real path is quite a mouthful, you've got to use a ContentResolver, here's how to retrieve it (based on that sample from Xamarin) :

    protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
        base.OnActivityResult (requestCode, resultCode, data);
        if ((resultCode == Result.Ok) && (requestCode == PickAudioId) && (data != null)) {

            string path = GetPathToImage(data.Data);

            if (!File.Exists (path)) {
                Log.Debug ("","error");
            } else {
                Log.Debug ("","ok");
            }
        }
    }

    private string GetPathToImage(Android.Net.Uri uri)
    {
        string path = null;
        // The projection contains the columns we want to return in our query.
        string[] projection = new[] { Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data };
        using (ICursor cursor = ManagedQuery(uri, projection, null, null, null))
        {
            if (cursor != null)
            {
                int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);
            }
        }
        return path;
    }

您还可以添加其他Android.Provider.MediaStore.Audio.Media.InterfaceConsts.XXX值,以获取有关该文件的更多信息

you can also add other Android.Provider.MediaStore.Audio.Media.InterfaceConsts.XXX values to get more info about the file

+

这篇关于Android文件路径(Xamarin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆