Simple Cordova plugin to save a pdf file in MediaStore.Downloads

Overview

com-thesis-plugins-pdfstore

This simple Cordova plugin saves a bytestring (the byte image of a pdf) to a pdf file in MediaStore.Downloads. The purpose is to allow the user to download a pdf file and open it using the device's default pdf viewer. Starting from android 10 and especially from android 11 with the introducion of 'scope storage' (https://developer.android.com/about/versions/11/privacy/storage) we need to change the way to save files in shared storages (https://developer.android.com/training/data-storage/shared) and go beyound the old filesystem approach of 'cordova-plugin-file'

This is our production example that uses the MediaStore Api.

Tested on Android APIs 28, 29(10 Q),30(11 R) and 31.

NOTE

Api level 29: using MediaStore Api with MediaStore.Downloads triggers 'SQLiteConstraintException: UNIQUE constraint failed' excpetions, so we use the legacy method (filesystem), you need to add android:requestLegacyExternalStorage="true" to your element in the manifest.

Api leved <= 29: add these permissions in your Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

API

The plugin exports only one function:

//CordovaAndroidThesisStore.js

/**
 * @param byteString the bytestring 
 * @param fileName 
 */
exports.store = function (byteString, fileName, success, error) {
    exec(success, error, 
        'CordovaAndroidThesisStore', 
        'store', 
        [byteString, fileDir, fileName]);
};

example


var array = new Uint8Array([37, 80, 68, 70, 45, 49, 46, 49, 10, 37, -62, -91, -62, -79, -61,
                                            -85, 10, 10, 49, 32, 48, 32, 111, 98, 106, 10, 32, 32, 60, 60, 32, 47, 84, 121, 112, 101, 32, 47, 67, 97, 116,
                                            97, 108, 111, 103, 10, 32, 32, 32, 32, 32, 47, 80, 97, 103, 101, 115, 32, 50, 32, 48, 32, 82, 10, 32, 32, 62,
                                            62, 10, 101, 110, 100, 111, 98, 106, 10, 10, 50, 32, 48, 32, 111, 98, 106, 10, 32, 32, 60, 60, 32, 47, 84, 121,
                                            112, 101, 32, 47, 80, 97, 103, 101, 115, 10, 32, 32, 32, 32, 32, 47, 75, 105, 100, 115, 32, 91, 51, 32, 48, 32,
                                            82, 93, 10, 32, 32, 32, 32, 32, 47, 67, 111, 117, 110, 116, 32, 49, 10, 32, 32, 32, 32, 32, 47, 77, 101, 100,
                                            105, 97, 66, 111, 120, 32, 91, 48, 32, 48, 32, 51, 48, 48, 32, 49, 52, 52, 93, 10, 32, 32, 62, 62, 10, 101,
                                            110, 100, 111, 98, 106, 10, 10, 51, 32, 48, 32, 111, 98, 106, 10, 32, 32, 60, 60, 32, 32, 47, 84, 121, 112,
                                            101, 32, 47, 80, 97, 103, 101, 10, 32, 32, 32, 32, 32, 32, 47, 80, 97, 114, 101, 110, 116, 32, 50, 32, 48, 32,
                                            82, 10, 32, 32, 32, 32, 32, 32, 47, 82, 101, 115, 111, 117, 114, 99, 101, 115, 10, 32, 32, 32, 32, 32, 32, 32,
                                            60, 60, 32, 47, 70, 111, 110, 116, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 60, 32, 47, 70, 49, 10,
                                            32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 60, 32, 47, 84, 121, 112, 101, 32, 47, 70, 111,
                                            110, 116, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 47, 83, 117, 98, 116,
                                            121, 112, 101, 32, 47, 84, 121, 112, 101, 49, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
                                            32, 32, 32, 47, 66, 97, 115, 101, 70, 111, 110, 116, 32, 47, 84, 105, 109, 101, 115, 45, 82, 111, 109, 97, 110,
                                            10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 62, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32,
                                            32, 32, 62, 62, 10, 32, 32, 32, 32, 32, 32, 32, 62, 62, 10, 32, 32, 32, 32, 32, 32, 47, 67, 111, 110, 116, 101,
                                            110, 116, 115, 32, 52, 32, 48, 32, 82, 10, 32, 32, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 10, 52, 32, 48,
                                            32, 111, 98, 106, 10, 32, 32, 60, 60, 32, 47, 76, 101, 110, 103, 116, 104, 32, 53, 53, 32, 62, 62, 10, 115,
                                            116, 114, 101, 97, 109, 10, 32, 32, 66, 84, 10, 32, 32, 32, 32, 47, 70, 49, 32, 49, 56, 32, 84, 102, 10, 32,
                                            32, 32, 32, 48, 32, 48, 32, 84, 100, 10, 32, 32, 32, 32, 40, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
                                            100, 41, 32, 84, 106, 10, 32, 32, 69, 84, 10, 101, 110, 100, 115, 116, 114, 101, 97, 109, 10, 101, 110, 100,
                                            111, 98, 106, 10, 10, 120, 114, 101, 102, 10, 48, 32, 53, 10, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 32, 54,
                                            53, 53, 51, 53, 32, 102, 32, 10, 48, 48, 48, 48, 48, 48, 48, 48, 49, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32,
                                            10, 48, 48, 48, 48, 48, 48, 48, 48, 55, 55, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48,
                                            48, 49, 55, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 48, 52, 53, 55, 32, 48, 48,
                                            48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 32, 32, 60, 60, 32, 32, 47, 82, 111, 111,
                                            116, 32, 49, 32, 48, 32, 82, 10, 32, 32, 32, 32, 32, 32, 47, 83, 105, 122, 101, 32, 53, 10, 32, 32, 62, 62, 10,
                                            115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 53, 54, 53, 10, 37, 37, 69, 79, 70, 10]);
    var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(array)));
    window.cordova.plugins.CordovaAndroidThesisStore.store(base64String, 'test3.pdf', function() {
        console.log('Excelsior!');
      }, function(err) {
        console.log('Uh oh... ' + err);
      });


/*
      //for large data
      var blob = new Blob([array]);
      var reader = new FileReader();
      reader.onload = reader.onload = (e) => {
         //var base64String =   e.target.result;
         var arr = (<string>e.target.result).split(',');
         var base64String = arr[arr.length-1];

         window.cordova.plugins.CordovaAndroidThesisStore.store(base64String, 'test3.pdf', function() {
        console.log('Excelsior!');
            }, function(err) {
                console.log('Uh oh... ' + err);
            });
            
      reader.readAsDataURL(blob);
      */
You might also like...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

May 21, 2022

Ethylene is a open-source, lightweight, general-purpose compatibility layer standing between the developer and the chaotic world of configuration file formats.

Ethylene Ethylene is a open-source, lightweight, general-purpose compatibility layer standing between the developer and the chaotic world of configura

Aug 9, 2022

UMS is a CRUD based management system which uses File Handling to manipulate data and perform the CRUD operations

UMS is a CRUD based management system which uses File Handling to manipulate data and perform the CRUD operations

UMS is a CRUD (Create, Read, Update, Delete) based management system which uses File Handling to manipulate data and perform the CRUD operations. It is a group project made using Java procedural programming having both User and Admin sides.

Dec 20, 2022

Generates a Proguard mapping file for use in obfuscating your Java projects.

Reaper Generates a Proguard mapping file for use in obfuscating your Java projects. Features Automatically checks for duplicate names. Interactive, in

Dec 29, 2022

LaetLang is an interpreted C style language. It has file reading/writting, TCP network calls and awaitable promises.

LaetLang 💻 LaetLang is an interpreted C style language built by following along Robert Nystrom's book Crafting Interpreters. This is a toy language t

Mar 14, 2022

With react-native-update-in-app library you can easily implement in-app updates in your React Native app using CDN or any other file server

React Native In-App update With react-native-update-in-app library you can easily implement in-app updates in your React Native app using CDN or any o

Dec 21, 2022

Operating Systems - Concepts of computer operating systems including concurrency, memory management, file systems, multitasking, performance analysis, and security. Offered spring only.

Nachos for Java README Welcome to Nachos for Java. We believe that working in Java rather than C++ will greatly simplify the development process by p

Nov 28, 2021

Drifty is an open-source interactive File Downloader system built with java. It is currently available in CLI mode and has the GUI version under active development.

Drifty is an open-source interactive File Downloader system built with java. It is currently available in CLI mode and has the GUI version under active development.

Drifty Drifty is an open-source interactive File Downloader system built using Java. It takes the link to the file, the directory where it needs to be

Dec 24, 2022

GodType is a very simple Bukkit plugin to allow the console (or a player) to chat as a defined name.

GodType GodType is a very simple Bukkit plugin to allow the console (or a player) to chat as a defined name. Config A config.yml file will be created

Dec 24, 2021
Owner
Alberto Ferrari
Alberto Ferrari
A script that downloads wallpapers from the subreddit r/wallpapers and then sets them on your Windows machine.

Wallpaper Downloader ?? This script takes the most voted wallpaper in the subreddit r/wallpapers in 24 hours, downloads the .json of that page, parses

404a10 1 May 15, 2022
TrashEmail is hosted Telegram bot that can save your private email address by offering disposable email address

TrashEmail is hosted Telegram bot that can save your private email address by offering disposable email address. It can create, manage, disposable email address and link them with your telegram bot chat.

Rohit Sehgal 493 Dec 28, 2022
Save local logcat via Java wiithout root

Save Logcat Android Simple way to save local logcat via Java wiithout root. It does not save full logcat from device, just only from the app. Save log

Menu Modder 5 Jun 30, 2022
Undo Rotate Crop Save

URCS_app Undo Rotate Crop Save The following project has these functionalities: Has 2 windows namely HOME and EDIT(fragments of MainActivity) HOME win

Utkarsh Sharma 1 Jan 23, 2022
Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, and other binary file formats.

Classpy Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, and other binary file formats. Inspiration This t

null 1k Dec 17, 2022
A simple program used to enter people into a file stored in memory, and the same saved data is displayed in a table

A simple program used to enter people (students or professors) into a file stored in memory, and the same saved data is displayed in a table. Persons have the appropriate attributes where name, surname, etc. are entered and identified by ID.

Ljubinko Stojanović 3 Apr 28, 2022
trying to create a plugin using the spigot api! this plugin will be responsible for delivering the products according to the settings!

KettraShop "simples plugin de ativação de produtos da loja, dentro do Minecraft" ⚙️ Configurações caso você não tenha uma loja virtual para seu servid

SEBASTIAN JN ฅ^•ﻌ•^ฅ 4 Nov 2, 2022
A singular file to protect as many Minecraft servers and clients as possible from the Log4j exploit (CVE-2021-44228).

MC-Log4J-Patcher The goal of this project is to provide Minecraft players, and server owners, peace of mind in regards to the recently discovered Log4

Koupa Taylor 4 Jan 4, 2022
Afnan007a's Original Ptero-VM made for Pterodactyl, running lightly in a .jar file.

Ptero-VM in a JAR. This project is a Java implementation of Afnan's Ptero-VM that was made for Pterodactyl, this project was created to support a even

Triassic 9 Oct 2, 2022
Gitlet - A version control system for managing file versions, mirroring the features of Git

Gitlet - A version control system for managing file versions, mirroring the features of Git. Emphasized the serialization and manipulation of files to compress, access, and manage file content efficiently. Stressed the employment of appropriate data structures and file persistance.

null 1 Jan 4, 2022