您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
10. JNI和c传递文件路径并设置app的读写权限~1
发布时间:2021-05-28 17:25:41编辑:雪饮阅读()
自動創建jni方法
#include <string>
#include <android/log.h>
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"testff",__VA_ARGS__)
extern "C"{
#include <libavcodec/avcodec.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndk_1and_141_MainActivity_stringFromJNI(JNIEnv* env,jobject) {
std::string hello = "Hello from C++";
hello+=avcodec_configuration();
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_ndk_1and_141_MainActivity_Open(JNIEnv *env, jobject thiz, jstring url_, jobject handle) {
const char *url=env->GetStringUTFChars(url_,0);
FILE *fp=fopen(url,"rb");
if(!fp){
LOGW("File %s open failed!",url);
}
else{
LOGW("File %s open success!",url);
fclose(fp);
}
}
#include <jni.h>package com.example.ndk_and_41;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
Open("/sdcard/1080.mp4",this);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native boolean Open(String url,Object handle);
}要打開文件,則要在清單文件上授權允許配置添加下: AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ndk_and_41">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ndk_and_41">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>然後上傳到待測試安卓設備上的sdcard根目錄一個1080.mp4文件
這裏測試安卓5.1 apis22 armeabiv7a
竟然發現一直卡在install過程中,難道我電腦太卡了。。。
實在沒有辦法了,又用真機魅族16t測試,自然是成功的
不過這裏需要補充的是cpp/native-lib.cpp:
#include <string>
#include <android/log.h>
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"testff",__VA_ARGS__)
extern "C"{
#include <libavcodec/avcodec.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndk_1and_141_MainActivity_stringFromJNI(JNIEnv* env,jobject) {
std::string hello = "Hello from C++";
hello+=avcodec_configuration();
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_ndk_1and_141_MainActivity_Open(JNIEnv *env, jobject thiz, jstring url_, jobject handle) {
const char *url=env->GetStringUTFChars(url_,0);
FILE *fp=fopen(url,"rb");
if(!fp){
LOGW("File %s open failed!",url);
return false;
}
else{
LOGW("File %s open success!",url);
fclose(fp);
return true;
}
}因爲java那邊定義的返回值是boolean,如果這裏不return,則程序會無法正常打開,雖然日志照樣生成(好像是,記得不太清楚了)
关键字词:JNI,java,c,權限