从外部存储读取数据库文件

问题描述 投票:0回答:2

因此,此程序从Firebase存储中下载文件,我的下载代码是:

private void downloadFiles(Context context, String fileName, String destinationDirectory, String url) {
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalFilesDir(context, String.valueOf(destinationDirectory), fileName);

    downloadManager.enqueue(request);
}

public void downloading(final String name) {
    downloadRef = storage.getInstance().getReference().child(name);
    downloadRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            String url = uri.toString();
            downloadFiles(Main2Activity.this, name,  getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), url);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(Main2Activity.this, "Failed!!!", Toast.LENGTH_LONG).show();
        }
    });
}

然后我想使用该数据库,因此我尝试使用以下方法打开它:

database = SQLiteDatabase.openDatabase(myPath, null, 0);

我该如何解决?这对他们真的很有帮助。

java android firebase firebase-storage
2个回答
0
投票

我以为您的问题是可访问性。这就是为什么我带回一个活动以获得用户的许可。您测试此代码

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                } else {
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    intent.putExtra("name", arrayList.get(position));
                    startActivity(intent);
                }

            }
        });

0
投票

您正在使用Environment.getExternalStorageState(),它返回主要共享/外部存储介质的当前状态。它不能用于获取路径作为任何文件的字符串。使用context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)获取Download目录的路径的字符串值。

© www.soinside.com 2019 - 2024. All rights reserved.