将文件下载到缓存文件夹中

时间:2017-10-23 15:40:04

标签: android caching permissions android-download-manager download-manager

大家好我正在尝试将OTA文件下载到缓存目录中。

try{ otaFile = new File(Environment.getDownloadCacheDirectory().getAbsolutePath() + File.separator + "update_unsigned" + ".zip");

                    Uri uri = Uri.parse(data.getFileUrl());

                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setTitle(data.getVersionUpdate()).
                            setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationUri(Uri.fromFile(otaFile));


                    downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);

                    downloadId = downloadManager.enqueue(request);

                    emitter.onSuccess("saving to " + otaFile + " id -  " + downloadId);
                } catch (Exception e) {
                    emitter.onError(new Throwable(e.getMessage()));
                }

当我调用此代码时,我面临异常: 不支持的路径/chche/update_unsigned.zip

我的应用具有以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />/
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />

我正在运行我的ap作为系统应用程序。

我也知道,下载管理器有隐藏方法setDestinationToSystemCache(),但是当我试图用反射得到它时我找不到它。

Class cls;
                    cls = Class.forName("android.app.DownloadManager");

                    Method m;

                    m = cls.getMethod("setDestinationToSystemCache", new Class[]{});
                    m.setAccessible(true);
                    m.invoke(null);

请帮助我找到将文件存储在缓存文件夹中的方法,因为它只是设备制造商可以接受的一个文件夹。

1 个答案:

答案 0 :(得分:-1)

您可以使用--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-119-5a5f11c97e39> in <module>() 29 left.add(Embedding(ENCODE_DIM, EMBED_DIM, input_length=1)) ---> 30 left.add(RepeatVector(look_back)) 31 32 leftOutput = left.output /usr/local/lib/python3.4/dist-packages/keras/models.py in add(self, layer) 467 output_shapes=[self.outputs[0]._keras_shape]) 468 else: --> 469 output_tensor = layer(self.outputs[0]) 470 if isinstance(output_tensor, list): 471 raise TypeError('All layers in a Sequential model ' /usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs) 550 # Raise exceptions in case the input is not compatible 551 # with the input_spec specified in the layer constructor. --> 552 self.assert_input_compatibility(inputs) 553 554 # Collect input shapes to build layer. /usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs) 449 self.name + ': expected ndim=' + 450 str(spec.ndim) + ', found ndim=' + --> 451 str(K.ndim(x))) 452 if spec.max_ndim is not None: 453 ndim = K.ndim(x) ValueError: Input 0 is incompatible with layer repeat_vector_9: expected ndim=2, found ndim=3 将文件保存在/ cache中。

request.setDestinationToSystemCache();将不允许您设置路径到/ cache之类的任何内部目录。方法定义表明它设置下载文件的本地目标。必须是外部存储路径的文件URI ,并且调用应用程序必须具有WRITE_EXTERNAL_STORAGE权限。