gpt4 book ai didi

java - 扩展文件 - 复制和使用

转载 作者:行者123 更新时间:2023-11-30 02:48:38 24 4
gpt4 key购买 nike

我一直在查看一些不同的教程,但我真的很难确切地了解如何首先将扩展文件复制到用户无法访问的位置,以及如何在我的实际应用中使用它们。

我将在扩展文件中包含很多关键图像,因此还需要阻止用户在下载所有内容之前玩游戏。

最后,我还必须为许多图像访问基于密度的部分,以及一些原始文件(如视频),这就是我目前必须确定在扩展文件中打开哪个文件夹.

String ExpansionFolder = "";
switch (getResources().getDisplayMetrics().densityDpi)
{
case DisplayMetrics.DENSITY_MEDIUM:
ExpansionFolder = "mdpi";
break;
case DisplayMetrics.DENSITY_HIGH:
ExpansionFolder = "hdpi";
break;
default: // This cover XHDPI, XXHDPI, TVDPI
ExpansionFolder = "xhdpi";
break;
}

一些我可以使用的示例代码将不胜感激。

最佳答案

我没有示例代码,因为我自己没有使用过扩展文件,但是您的问题的具体答案似乎可以在 Android Developer APK Expansion Files page 找到。 .

how the Expansion Files can be firstly copied to a location that the user cannot access adn the how to use them in my actual app.

来自 Android 开发者页面关于扩展文件的“存储位置”部分 (APK Expansion Files):

When Google Play downloads your expansion files to a device, it saves them to the system's shared storage location. To ensure proper behavior, you must not delete, move, or rename the expansion files. In the event that your application must perform the download from Google Play itself, you must save the files to the exact same location.

The specific location for your expansion files is:

[shared-storage]/Android/obb// [shared-storage] is the path to the shared storage space, available from getExternalStorageDirectory(). [package-name] is your application's Java-style package name, available from getPackageName(). For each application, there are never more than two expansion files in this directory. One is the main expansion file and the other is the patch expansion file (if necessary). Previous versions are overwritten when you update your application with new expansion files.

If you must unpack the contents of your expansion files, do not delete the .obb expansion files afterwards and do not save the unpacked data in the same directory. You should save your unpacked files in the directory specified by getExternalFilesDir(). However, if possible, it's best if you use an expansion file format that allows you to read directly from the file instead of requiring you to unpack the data. For example, we've provided a library project called the APK Expansion Zip Library that reads your data directly from the ZIP file.

Note: Unlike APK files, any files saved on the shared storage can be read by the user and other applications.

来自 http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) :

获取外部文件目录

Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.getExternalStorageDirectory()) where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.


I will have a lot of key images within the Expansion file and therefore would also need to prevent users from playing the game until everything is downloaded.

来自 APK Expansion Files 的“下载扩展文件”部分:

In most cases, Google Play downloads and saves your expansion files to the device at the same time it installs or updates the APK. This way, the expansion files are available when your application launches for the first time. However, in some cases your app must download the expansion files itself by requesting them from a URL provided to you in a response from Google Play's Application Licensing service.

The basic logic you need to download your expansion files is the following:

When your application starts, look for the expansion files on the shared storage location (in the Android/obb/[package-name]/ directory).

  1. If the expansion files are there, you're all set and your application can continue.
  2. If the expansion files are not there: a. Perform a request using Google Play's Application Licensing to get your app's expansion file names, sizes, and URLs. b. Use the URLs provided by Google Play to download the expansion files and save the expansion files. You must save the files to the shared storage location (Android/obb/[package-name]/) and use the exact file name provided by Google Play's response.

Note: The URL that Google Play provides for your expansion files is unique for every download and each one expires shortly after it is given to your application.

In addition to the LVL, you need a set of code that downloads the expansion files over an HTTP connection and saves them to the proper location on the device's shared storage. As you build this procedure into your application, there are several issues you should take into consideration:

  • The device might not have enough space for the expansion files, so you should check before beginning the download and warn the user if there's not enough space.
  • File downloads should occur in a background service in order to avoid blocking the user interaction and allow the user to leave your app while the download completes.
  • A variety of errors might occur during the request and download that you must gracefully handle.
  • Network connectivity can change during the download, so you should handle such changes and if interrupted, resume the download when possible.
  • While the download occurs in the background, you should provide a notification that indicates the download progress, notifies the user when it's done, and takes the user back to your application when selected.

To simplify this work for you, we've built the Downloader Library, which requests the expansion file URLs through the licensing service, downloads the expansion files, performs all of the tasks listed above, and even allows your activity to pause and resume the download. By adding the Downloader Library and a few code hooks to your application, almost all the work to download the expansion files is already coded for you. As such, in order to provide the best user experience with minimal effort on your behalf, we recommend you use the Downloader Library to download your expansion files. The information in the following sections explain how to integrate the library into your application.

您还可以找到 Steps to create APK expansion file很有用,如果你还没有看到它的话,虽然我不知道自从那个问题及其答案发布后谷歌是否改变了任何关于扩展文件的内容。

关于java - 扩展文件 - 复制和使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24600795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com