我知道我们可以从即时应用程序将数据传输到使用谷歌即时提到here的存储API的全面应用。
对于运行OS版本比奥利奥较低的设备,我想读取数据如下:
public void getInstantAppData(final Activity activity, final InstantAppDataListener listener) {
InstantApps.getInstantAppsClient(activity)
.getInstantAppData()
.addOnCompleteListener(new OnCompleteListener<ParcelFileDescriptor>() {
@Override
public void onComplete(@NonNull Task<ParcelFileDescriptor> task) {
try {
FileInputStream inputStream = new FileInputStream(task.getResult().getFileDescriptor());
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
Log.i("Instant-app", zipEntry.getName());
if (zipEntry.getName().equals("shared_prefs/")) {
extractSharedPrefsFromZip(activity, zipEntry);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void extractSharedPrefsFromZip(Activity activity, ZipEntry zipEntry) throws IOException {
File file = new File(activity.getApplicationContext().getFilesDir() + "/shared_prefs.vlp");
mkdirs(file);
FileInputStream fis = new FileInputStream(zipEntry.getName());
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream stream = new ZipInputStream(bis);
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
int length;
while ((length = stream.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
}
但我基本上得到一个错误Method threw 'java.io.FileNotFoundException' exception.
当我尝试读取shared_pref文件是不能够找到它。什么是文件的全名,有没有更好的办法从即时应用转移我的共享PREF数据安装的应用。
花几个小时后,我能够使它工作,但后来我发现了一个更好更简单的方式来做到这一点。谷歌也有一个饼干API,它可以被用来从即时应用程序中的数据分享给你完整的应用程序时,用户升级。
示例:https://github.com/googlesamples/android-instant-apps/tree/master/cookie-api
我更喜欢这个,因为它是干净多了,容易实现,但最重要的事情是,你不必增加你的安装应用程序的目标沙箱版本2,如果你使用存储API,它是必需的。它与操作系统版本大于或等于8,以及与OS版本低于8设备的设备。
希望这可以帮助别人。