【发布时间】:2018-11-03 17:39:47
【问题描述】:
当我在 Android Studio 中启动我的应用程序时,我必须将我的资产文件夹中的文件转换为文件,以便将其作为 RDF 模型读取。问题是我的文件现在越来越大,应用程序通常会崩溃,或者文件下载时间过长。该文件大约为 170.384 MB,因此它是一个相当大的文件,但我希望在完成之前该文件会增长到 1GB 左右。我已经在这里搜索了有关如何更快地下载文件的答案,但是我没有看到任何我没有尝试过的新东西。例如,我启动了一个 AsyncTask,以便在与主 UI 线程不同的线程上执行“下载”操作。这阻止了我的应用程序崩溃,但文件仍然需要很长时间才能下载并且通常会超时。这是我在 AsyncTask 中下载文件的代码示例:
@Override
public Model doInBackground(String... params){
Model tdb = null;
try {
String filePath = context.getFilesDir() + File.separator + "my_turtle.ttl";
File destinationFile = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("sample_3.ttl");
byte[] buffer = new byte[1024];
int length = 0;
int per = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
publishProgress(per);
per++;
}
inputStream.close();
outputStream.close();
Dataset dataset = TDBFactory.createDataset();
tdb = dataset.getNamedModel("my_dataset.ttl");
///Model m = FileManager.get().loadModel(FileManager.get().readWholeFileAsUTF8(inputStream));
TDBLoader.loadModel(tdb, filePath, false);
model = tdb;
MainActivity.presenter.setModel(model);
}catch(java.io.FileNotFoundException e){
e.printStackTrace(System.out);
}
catch(java.io.IOException e){
e.printStackTrace(System.out);
}
return tdb;
}
如您所见,我必须创建一个新的 filePath 并创建一个新文件,创建一个 InputStream 以便从资产中读取文件,并将 InputStream 写入该文件以便在 TDB 存储中加载 RDF 模型。正是在这个过程中,我的应用程序开始在 AsyncTask 中“超时”。
我遇到的另一个问题是在加载文件时更新我的进度条。我不知道如何获得 InputStream 的长度,我在我的代码中做这样的事情:
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
publishProgress(per); //Publishing progress here...
per++;
}
发布进度应该触发onProgressUpdate方法():
@Override
public void onProgressUpdate(Integer... progress){
progressBar.setProgress(progress[0]);
pd.setProgress(progress[0]);
}
但由于某种原因,我的 ProgressBar 和 ProgressDialog 在整个上传时间都保持在 0%(即使我使用要上传的较小文件)。我不确定为什么会这样。
下面是我的 AsyncTask 类的完整代码。我想发布代码的相关部分,但它们可能是我的代码更广泛的上下文中导致问题的内容:
public class DownloadModel extends AsyncTask<String, Integer, Model> {
Context context;
Model model = null;
ProgressDialog pd;
public DownloadModel(Context context){
this.context = context;
this.pd = new ProgressDialog(context);
}
@Override
public Model doInBackground(String... params){
Model tdb = null;
try {
String filePath = context.getFilesDir() + File.separator + "my_turtle.ttl";
File destinationFile = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("sample_3.ttl");
byte[] buffer = new byte[10000000];
int length = 0;
int per = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
publishProgress(per);
per++;
}
inputStream.close();
outputStream.close();
Dataset dataset = TDBFactory.createDataset();
tdb = dataset.getNamedModel("my_dataset.ttl");
///Model m = FileManager.get().loadModel(FileManager.get().readWholeFileAsUTF8(inputStream));
TDBLoader.loadModel(tdb, filePath, false);
model = tdb;
MainActivity.presenter.setModel(model);
}catch(java.io.FileNotFoundException e){
e.printStackTrace(System.out);
}
catch(java.io.IOException e){
e.printStackTrace(System.out);
}
return tdb;
}
@Override
public void onProgressUpdate(Integer... progress){
progressBar.setProgress(progress[0]);
pd.setProgress(progress[0]);
}
@Override
public void onPreExecute(){
pd.setMessage("Loading Knowledgebase...");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.show();
}
@Override
public void onPostExecute(Model i){
progressBar.setVisibility(View.GONE);
pd.dismiss();
}
}
感谢您的帮助!
【问题讨论】:
-
@CommonsWare 因为在 Androjena 中没有采用输入流的 .load() 方法。
-
@CommonsWare 我希望有一个替代方案。你说的会容易得多。例如,在 Jena 3.7 中,我通常会做一些事情,例如 RDFParser.create().source(inputStream)... 或类似的事情。我在 Androjena 找不到类似的东西
-
@CommonsWare 是的,我昨天检查了 4 岁的选项。我找不到可靠的端口,这就是导致我来到这里的原因哈哈。
标签: java android android-studio rdf jena