NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
102 stars 50 forks source link

Getting this to work in android #169

Closed ihazar closed 5 years ago

ihazar commented 6 years ago

I can't get this plugin to work.

I used this: tns create sendFile-http-haz-ng3 --ng and then installed this plugin:

tns plugin add nativescript-background-http I didn't change anything at all, and ran this:

tns run android and tns run ios

On iOS it runs OK, but on Android I get the error described below. Any ideas how to fix this please before I abandon NativeScript? Thanks

Preparing project... Project successfully prepared (Android) Successfully transferred app.module.js. Successfully transferred item-detail.component.js. Successfully transferred app.routing.js. Refreshing application... Successfully synced application org.nativescript.sendFilehttphazng3 on device emulator-5554. JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode. Executing before-prepare hook from /Users/ihazar/code/test03/sendFile-http-haz-ng3/hooks/before-prepare/nativescript-dev-typescript.js Hook skipped because either bundling or livesync is in progress. Preparing project... Successfully prepared plugin nativescript-angular for android. Successfully prepared plugin nativescript-background-http for android. Successfully prepared plugin nativescript-theme-core for android. Successfully prepared plugin tns-core-modules for android. Successfully prepared plugin nativescript-intl for android. Successfully prepared plugin tns-core-modules-widgets for android. Project successfully prepared (Android) Building project... Gradle build...

  • setting applicationId
  • applying user-defined configuration from /Users/ihazar/code/test03/sendFile-http-haz-ng3/app/App_Resources/Android/app.gradle
  • adding nativescript runtime package dependency: nativescript-optimized-with-inspector
  • adding aar plugin dependency: /Users/ihazar/code/test03/sendFile-http-haz-ng3/node_modules/nativescript-background-http/platforms/android/nativescript_background_http.aar
  • adding aar plugin dependency: /Users/ihazar/code/test03/sendFile-http-haz-ng3/node_modules/tns-core-modules-widgets/platforms/android/widgets-release.aar /Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values-v26/values-v26.xml:9:5-12:13: AAPT: error: resource android:attr/colorError not found.

/Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values-v26/values-v26.xml:13:5-16:13: AAPT: error: resource android:attr/colorError not found.

/Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values-v26/values-v26.xml:17:5-93: AAPT: error: style attribute 'android:attr/keyboardNavigationCluster' not found.

/Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values/values.xml:252:5-69: AAPT: error: resource android:attr/fontStyle not found.

/Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values/values.xml:252:5-69: AAPT: error: resource android:attr/font not found.

/Users/ihazar/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.0.2.aar/4b8481eb39d2b29895202e50256a8a14/res/values/values.xml:252:5-69: AAPT: error: resource android:attr/fontWeight not found.

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:processDebugResources'.

    Failed to process resources, see aapt output above for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 5s Unable to apply changes for device: emulator-5554. Error is: Command ./gradlew failed with exit code 1. Executing after-watch hook from /Users/ihazar/code/test03/sendFile-http-haz-ng3/hooks/after-watch/nativescript-dev-typescript.js Stopping tsc watch

NickIliev commented 6 years ago

@ihazar what API level is your emulator/device? Try removing the node_modules, platforms and hooks folders and rebuild your project.

The issue is most likely related to the fact that you are using anewer API level device/emulator and you are lacking the SDK version - try installing SDK 26 in your Android SDK manager and make clean rebuild (the step above)

rohanrichards commented 6 years ago

I had the above issue, my emulator is running API25. I have installed through the SDK manager API's 25, 26, 27. I installed SDK Tools: Android SDK Build-Tools, Android SDK Platform-Tools, Android SDK Tools. (I also updated emulator/haxm/google play services) I have removed node_modules, hooks and platform folders and rebuilt my project (npm i, tns platform add android) I created a new emulator device running SDK 27 (This prompted me to download SDK27 even though I had it installed through SDK manager) I have also tried setting the target SDK in nativescript sidekick to 27

Running tns debug android seems to be working fine. It is also now building in NativeScript Sidekick

RyamBaCo commented 6 years ago

Had the exact same issue as I was using the SDK-Version 25 for building my app. My build tools where v27.0.3 so I also installed the platform SDK for 27 by calling

sudo $ANDROID_HOME/tools/bin/sdkmanager "tools" "emulator" "platform-tools" "platforms;android-27" "build-tools;27.0.3" "extras;android;m2repository" "extras;google;m2repository"

and now it works!

jenyaivanova521 commented 5 years ago

this code does not work for me can you help me to solve the issue?

let session = bghttp.session('post');
let params = [];
const image = this.image;
console.log(image); //print as '/storage/emulated/0/DCIM/Camera/NSIMG_20181009_235736.jpg'
const param = {
     name: "photos",
     mimeType: 'image/jpeg',
     fileName: image,
};
params.push(param);
const url = API_URL + '/posts';
let request = {
 url: url,
 method: 'POST',
 headers: {
     'Authorization': 'Bearer ' + this.authService.getToken(),
     },
     description:"Post Upload"
 };
 let task: bghttp.Task;     
 task = session.multipartUpload(params, request);
 task.on('responded', this.onCompleteUpload);
 task.on('error', this.onUploadError)

and I get this response

{
    "eventName": "error",
    "object": {
        "_observers": {
            "responded": [
                {}
            ],
            "error": [
                {}
            ]
        },
        "_session": {
            "_id": "post"
        },
        "_id": "post{3}",
        "_description": "Post Upload",
        "_upload": 153,
        "_totalUpload": 153,
        "_status": "error"
    },
    "error": null,
    "responseCode": 400,
    "response": {}
}

maybe image path is wrong?

NickIliev commented 5 years ago

@jenyaivanova521 I guess you are taking images with the device camera (or directly looking in the camera folder). In this case, you must save your image to an accessible folder. Then you could access the saved file and upload it.

Basic example:

import { File, Folder, path, knownFolders } from "tns-core-modules/file-system";

// to access the `android` native API use `tns-platform-declarations`

let androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES).toString();
// instaed of accessing the DIRECTORY_PICTURES via native Android API, you could use the NativeScript knownFolders
// let downloadPath = knownFolders.documents();

myFolderPath = path.join(
    androidDownloadsPath,
    "MyFolderName"
);

let folder: Folder = Folder.fromPath(myFolderPath );
let myPath: string = path.join(myFolderPath , fileName);
let exists: boolean = File.exists(myPath);

if (!exists) {
    let saved: boolean = imageSource.saveToFile(myPath, "jpg");
}

based on this logic.

jenyaivanova521 commented 5 years ago

Thank you for your advice can you attach sample accessible folder path? it is correct path?

/data/data/org.nativescript.nativescriptapp/files/app/NSIMG_20181011_102646.jpg
NickIliev commented 5 years ago

The Pictures folder is one example

let androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES).toString();

or the documents folder which can be access directly via NativeScript

import { knownFolders } from "tns-core-modules/file-system";
let documentsFolder = knownFolders.documents();

By accessible I've meant a folder in which you would have write/delete access (so you could save your image taken from the camera for example). The path that you are showing is your application a path - you can read the image but you can't save or delete a file in any application folder.