Android API 26(Oreo)从互联网下载数据文件

时间:2018-07-09 16:16:38

标签: android android-8.0-oreo

我现在已经解决了这个问题,事实证明这完全不同。由于没有人回应,因此可以删除问题。

要使用Android Studio向前支持API 26,我已经更新了我的应用程序,以明确询问我需要的危险权限(WRITE_EXTERNAL_STORAGE,ACCESS_FINE_LOCATION)。它还使用普通权限INTERNET,无需询问用户即可授予该权限。

该应用程序在首次运行时会尝试从承载这些文件的特定网站上将某些数据文件(所有文本)下载到本地存储中。尽管它在API 27模拟器中可以正常工作,但在运行Android 8.1.0(Huawei P20)的手机上进行测试时,它却失败了。

两个文件已成功下载,但在第三个文件上,当在连接上调用getInputStream时,它会引发IO异常“文件的结尾过早”,传输0字节,并且该应用程序在显示失败消息后退出。但是,如果我随后在手机的应用程序设置中清除数据,然后在手机上重新启动应用程序,则所有文件的完全下载成功。

调用此函数下载每个文件(else子句是相关的):

// Download file from the internet
public static boolean downloadFile(String urlString, String fileName)
{
    mDownloadStatus = false;

    // In local file mode, download.tmp must have been pre-loaded
    if (LOCAL_FILE_MODE && fileName.equals(LOCAL_FILE_NAME))
    {
        File f = new File(dataDir, LOCAL_FILE_NAME);
        if (f.exists())
            mDownloadStatus = true;
        else
            mErrorString = "Pre-loaded " + LOCAL_FILE_NAME + " not found";
    }
    else
    {
        CountDownLatch latch = new CountDownLatch(1);
        Runnable r = new DownloadFile(urlString, fileName, latch);
        new Thread(r).start();

        try
        {
            // Wait for completion
            latch.await();
        }
        catch (InterruptedException e) {}
    }
    return mDownloadStatus;
}

这是DownloadFile类:

public class DownloadFile implements Runnable
{
    private String          mUrlString;
    private String          mFileName;
    private CountDownLatch  mLatch;

    public DownloadFile(String urlString, String fileName, CountDownLatch latch)
    {
        mUrlString  = urlString;
        mFileName   = fileName;
        mLatch      = latch;
    }

    public void run()
    {
        HttpURLConnection urlConnection = null;

        Spine.mDownloadStatus = false;

        try
        {
            // set the download URL, a url that points to a file on the internet
            // this is the file to be downloaded
            URL url = new URL(mUrlString);

            // create the new connection
            urlConnection = (HttpURLConnection) url.openConnection();

            // set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);

            // and connect!
            urlConnection.connect();

            // create a new file, specifying the path, and the filename
            // which we want to save the file as.
            File file = new File(Spine.dataDir, mFileName);

            // this will be used to write the downloaded data into the file we
            // created
            FileOutputStream fileOutput = new FileOutputStream(file);

            // this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            // create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; // used to store a temporary size of the buffer

            // now, read through the input buffer and write the contents to the
            // file
            while ((bufferLength = inputStream.read(buffer)) > 0)
            {
                // add the data in the buffer to the file in the file output
                // stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
            }

            // close the output stream when done
            fileOutput.close();

            Spine.mDownloadStatus = true;
        }

        // catch some possible errors...
        catch (IOException e)
        {
            Spine.mErrorString = e.getMessage();
        }

        if (urlConnection != null)
            urlConnection.disconnect();

        // Signal completion
        mLatch.countDown();
    }
}

该如何解决?我快要死了!

1 个答案:

答案 0 :(得分:0)

更改代码以更正确地处理权限请求后,描述的原始问题不再存在。但是,我有一个完全不同的问题可以解决。问题概述:在调试和发行版本中都使用API​​27仿真器。在华为P20(API27)上进行调试,但发布失败。一旦发现,便可以为发布版本启用调试功能,便可以找到问题并进行修复。

即使已卸载该应用程序,该应用程序的

SharedPreferences仍会以某种方式保留在电话上。问题在于,SharedPreferences表示某些文件当然已经下载了,当然它们在手机上已经不存在了。我的解决方法是在这种情况下检查文件是否存在,如果不存在,我会重置首选项,以便进行下载。

(我怀疑它是从较早版本的应用中获取以前的SharedPreferences,并从Google Play商店安装并卸载了,因此我可以测试新版本。)