Categories
discuss

JNI: Can not get array length

I faced with the next problem: I can not do anything with byte[] (jbyteArray) in C code. All functions that work with array in JNI cause JNI DETECTED ERROR IN APPLICATION: jarray argument has non-array type. What’s wrong with my code?

C:

#include <stdio.h>
#include <jni.h>

static jstring convertToHex(JNIEnv* env, jbyteArray array) {
    int len = (*env)->GetArrayLength(env, array);// cause an error;
    return NULL;
}

static JNINativeMethod methods[] = {
    {"convertToHex", "([B)Ljava/lang/String;", (void*) convertToHex },
};

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JNIEnv* env = NULL;

    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        return -1;
    }

    jclass cls = (*env)->FindClass(env, "com/infomir/stalkertv/server/ServerUtil");

    (*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0]) );

    return JNI_VERSION_1_4;
}

ServerUtil:

public class ServerUtil {

    public ServerUtil() {
        System.loadLibrary("shadow");
    }

    public native String convertToHex(byte[] array);
}

Main Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ServerUtil serverUtil = new ServerUtil();
        byte[] a = new byte[]{1,2,3,4,5};
        String s = serverUtil.convertToHex(a);
    }
}

My environment:

  • Android Studio 2.0
  • Experimental Gradle plugin 0.7.0
  • JAVA 1.8
  • NDK r11b
  • Windows 10 x64

Thanks in advance!

Answer

The second argument passed to your function isn’t a jbyteArray.

Per the JNI documentation, the arguments passed to a native function are:

Native Method Arguments

The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or nonstatic. The second argument to a nonstatic native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

The remaining arguments correspond to regular Java method arguments. The native method call passes its result back to the calling routine via the return value.

Your jstring convertToHex(JNIEnv* env, jbyteArray array) is missing the second jclass or jobject argument, so you’re treating either a jobject or jclass argument and a jbyteArray.

Categories
discuss

Link to manage Play Store subscription

On iOS, we can use https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/manageSubscriptions, and it will open the native subscription manager.

Is this possible to do this with the Play Store, or is there any other possible way to open the native subscription manager for Google Play Store?

Same as this question, but for Android: Link to app manage subscriptions in app store.

Answer

I use Action view to open Google Play Store -> Account.

    private fun openPlaystoreAccount() {
        try {
            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/account?utm_source=google&utm_medium=account&utm_campaign=my-account")));
        } catch (e: ActivityNotFoundException) {
            showToast("Cant open the browser")
            e.printStackTrace()
        } 
    }

Update:

Google released a new deep link, where it will take users directly to your app manage subscription page. You need two things SKU and your app package name.

Example URL:

https://play.google.com/store/account/subscriptions?sku=yoursku&package=com.yourpackagename

Sample Code in Kotlin:

 private val packageName = "com.mydomain.myapp"
 private val sku = "mySku"

 private fun openPlaystoreAccount() {
     try {
         startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/account/subscriptions?sku=$sku&package=$packageName")))
     } catch (e: ActivityNotFoundException) {
         showToast("Cant open the browser")
         e.printStackTrace()
     }
 }
Categories
discuss

Download Manager not working

I’m trying to develop app that show videos and you can Download it i’m using Download Manager class but it didn’t work, also it didn’t give me any error 🙁

this is my download manager code:

    public void downloadFileFromUrl(String url, String fileName) {

        String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";

        File folder = new File(filePath);

        if (!folder.exists()) {
            folder.mkdirs();
        }

        try {

        Uri downloadUri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.allowScanningByMediaScanner();

        request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setVisibleInDownloadsUi(true);
        DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
        long id= downloadManager.enqueue(request);
            Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
            Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

        }

        catch (Exception ex){
            Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
        }
    }

and this is how I’m calling it

downloadFileFromUrl(path, fileName);

where:

path: “192.168.1.5:8080/BlueNet_NMC/blue_elephant.mp4”

filename: “blue_elephant.mp4”

and i already give this permissions to manifests

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

so please any help

Answer

As I said in the comments, DownloadManager only handles requests starting with http:// or https:// as you can see in the docs.

I don’t know exactly what’s the problem because I lack information about your server, but I think it’s a common issue, so you should avoid using an IP address without providing that scheme.

Categories
discuss

Angular 2: import external js file into component

I’m going to import this d3gauge.js file into one of my angular2 component, memmon.component.ts file.

import '../../../../js/d3gauge.js';
export class MemMonComponent {
    createMemGauge() {
        new drawGauge(this.opt);  //drawGauge() is a function inside d3gauge.js
    }
}

and in the corresponding template file, add

<script src="../../../../js/d3gauge.js"></script>

But it doesn’t work, drawGaugecan’t be found.

So,

  1. what’re correct steps to import an external js file to angular2?
  2. since I’m using webpack, is it possible to do it in webpack? I refer to this question , the webpack solution there doesn’t work for me as a result of .ensure can’t be resolved.

Answer

Ideally you need to have .d.ts file for typings to let Linting work.

But It seems that d3gauge doesn’t have one, you can Ask the developers to provide and hope they will listen.


Alternatively, you can solve this specific issue by doing this

declare var drawGauge: any;

import '../../../../js/d3gauge.js';
export class MemMonComponent {
    createMemGauge() {
        new drawGauge(this.opt);  //drawGauge() is a function inside d3gauge.js
    }
}

If you use it in multiple files, you can create a d3gauage.d.ts file with the content below

declare var drawGauge: any;

and reference it in your boot.ts (bootstrap) file at the top, like this

///<reference path="../path/to/d3gauage.d.ts"/>
Categories
discuss

Intellij doesn’t show run button

Intellij doesn’t show run button even if that file is in src folder.

no run button

Maybe do you know what to do?

P. s. It’s strange because that file used to show run button but after recloning git repository it just doesn’t show any more.

Answer

Right click on src directory and select option Mark Directory As > Sources Root.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..