简体   繁体   中英

Reading Database File from External Storage

So, this program downloads a file from firebase storage, my downloading code is:

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();
        }
    });
}

and then I want to use this database so I tried to open it with:

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

How can I solve this one? It would be really helpful guys.

I thought your problem was accessibility. That's why I brought a activity back to get permission from the user. You test this code

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);
                }

            }
        });

You are using Environment.getExternalStorageState() which returns the current state of the primary shared/external storage media. It cannot be used to get the path as string of any file. Use context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) to get the string value of the path to the Download directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM