在nexus模拟器上使用android api 23从gallary获取图像真实路径

时间:2017-10-01 21:03:51

标签: android android-intent android-gallery

这就是我打开gallary的方式

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

public static class Example
{
    private class Dto
    {
        public Dto(string filePath, byte[] data)
        {
            FilePath = filePath;
            Data = data;
        }

        public string FilePath { get; }
        public byte[] Data { get; }
    }

    public static async Task ProcessFiles(string path, IProgress<ProgressReport> progress)
    {
        int totalFilesFound = 0;
        int totalFilesRead = 0;
        int totalFilesHashed = 0;
        int totalFilesUploaded = 0;

        DateTime lastReported = DateTime.UtcNow;

        void ReportProgress()
        {
            if (DateTime.UtcNow - lastReported < TimeSpan.FromSeconds(1)) //Try to fire only once a second, but this code is not perfect so you may get a few rapid fire.
            {
                return;
            }
            lastReported = DateTime.UtcNow;
            var report = new ProgressReport(totalFilesFound, totalFilesRead, totalFilesHashed, totalFilesUploaded);
            progress.Report(report);
        }


        var getFilesBlock = new TransformBlock<string, Dto>(filePath =>
        {
            var dto = new Dto(filePath, File.ReadAllBytes(filePath));
            totalFilesRead++; //safe because single threaded.
            return dto;
        });

        var hashFilesBlock = new TransformBlock<Dto, Dto>(inDto =>
            {
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    var outDto = new Dto(inDto.FilePath, md5.ComputeHash(inDto.Data));
                    Interlocked.Increment(ref totalFilesHashed); //Need the interlocked due to multithreaded.
                    ReportProgress();
                    return outDto;
                }
            },
            new ExecutionDataflowBlockOptions{MaxDegreeOfParallelism = Environment.ProcessorCount, BoundedCapacity = 50});

        var writeToDatabaseBlock = new ActionBlock<Dto>(arg =>
            {
                //Write to database here.
                totalFilesUploaded++;
                ReportProgress();
            },
            new ExecutionDataflowBlockOptions {BoundedCapacity = 50});

        getFilesBlock.LinkTo(hashFilesBlock, new DataflowLinkOptions {PropagateCompletion = true});
        hashFilesBlock.LinkTo(writeToDatabaseBlock, new DataflowLinkOptions {PropagateCompletion = true});

        foreach (var filePath in Directory.EnumerateFiles(path))
        {
            await getFilesBlock.SendAsync(filePath).ConfigureAwait(false);
            totalFilesFound++;
            ReportProgress();
        }

        getFilesBlock.Complete();

        await writeToDatabaseBlock.Completion.ConfigureAwait(false);
        ReportProgress();
    }
}

public class ProgressReport
{
    public ProgressReport(int totalFilesFound, int totalFilesRead, int totalFilesHashed, int totalFilesUploaded)
    {
        TotalFilesFound = totalFilesFound;
        TotalFilesRead = totalFilesRead;
        TotalFilesHashed = totalFilesHashed;
        TotalFilesUploaded = totalFilesUploaded;
    }

    public int TotalFilesFound { get; }
    public int TotalFilesRead{ get; }
    public int TotalFilesHashed{ get; }
    public int TotalFilesUploaded{ get; }
}

这是我的onActvitityResult

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);

这就是我试图从gallary获得真正的路径,但它没有返回任何东西,String返回的是空的。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Disc.setText(selectedImagePath);
            }
        }
    }

任何人都可以帮助我吗,我一直在网上搜索但没有任何工作

2 个答案:

答案 0 :(得分:0)

A Uri is not a fileACTION_GET_CONTENT没有要求Uri返回来自MediaStore的{​​{1}}。 ACTION_GET_CONTENT无需向您返回指向文件的Uri

删除getPath()方法。使用ContentResolveropenInputStream()来阅读Uri标识的内容。

答案 1 :(得分:0)

发现你需要为android api 19及更高版本使用运行时permision,它现在完美运行:)