Android NDK의 파일 작업
성능상의 이유로 주로 C로 응용 프로그램을 만드는 데 Android NDK를 사용하고 있지만 fopen과 같은 파일 작업이 Android에서 제대로 작동하지 않는 것 같습니다. 이러한 기능을 사용하려고 할 때마다 응용 프로그램이 충돌합니다.
Android NDK로 파일을 생성 / 쓰기하려면 어떻게합니까?
다른 답변은 정확합니다. 당신은 사용 NDK를 통해 파일을 열 수 FILE
하고 fopen
있지만, 이에 대한 권한을 배치하는 것을 잊지 마세요.
Android 매니페스트 위치 :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
파일 IO는 JNI를 사용하는 Android에서 잘 작동합니다. 잘못된 경로로 파일을 열려고하고 리턴 코드를 확인하지 않았을 수 있습니다. hello-jni 예제를 수정하여 파일을 열고 쓸 수 있음을 보여주었습니다. 이게 도움이 되길 바란다.
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string.h>
#include <jni.h>
#include <stdio.h>
/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
*/
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
FILE* file = fopen("/sdcard/hello.txt","w+");
if (file != NULL)
{
fputs("HELLO WORLD!\n", file);
fflush(file);
fclose(file);
}
return (*env)->NewStringUTF(env, "Hello from JNI (with file io)!");
}
다음은 내 휴대 전화 (SD 카드 사용)에서 실행 한 후의 결과입니다.
$ adb -d shell cat /sdcard/hello.txt
HELLO WORLD!
Make sure to use the Java getExternalStorageDirectory()
call to get the real path to the sdcard since newer devices don't simply map it to "/sdcard". In that case trying to use a hardcoded location of "/sdcard" will fail.
I can also verify that fopen() works correctly, but not if you're trying to access a file in the application's resources or assets folder. I recommend, to avoid having to reinvent the wheel, that you stick any assets you want shipped with your app in the assets folder, where they'll be packaged up for distribution.
In the assets folder case you need to do one of two things, depending on whether the file was compressed by the packager. Both use the AssetManager methods, and you can get the AssetManager from the context/app. File names are always relative to the assets folder, btw: If your have a file "foo.png" directly in the assets folder, you'd open "foo.png," not something like "assets/foo.png".
If the file wasn't compressed (i.e., it's one of the extensions that doesn't get compressed, like .png), you can get a file descriptor from AssetManager.openFd() and pass it to C++. Then you can use fdopen(dup(fd),"r"); to open the file as a FILE*. Note you must fseek() to the offset, and keep track of the length of the file yourself. You're really getting a file handle to the entire assets package, and your file of interest is only a small part.
If your file is compressed, you need to use the Java streaming reader: AssetManager.open() gives you an InputStream you can use the read the file in. This is a PITA because you can't query (AFAIK) the file size; I run a preprocessing step on my assets folder that generates a list of all the files with their respective sizes so I can know, e.g., how big of a buffer to allocate.
If your file is a resource, you may need to go through the Resource class to access it, though it appears resources are also packed into the same assets package. Resource has an openRawResource() call to get the InputStream and an openRawResourceFd() call to get the file descriptor, as above, though.
Good luck.
참고URL : https://stackoverflow.com/questions/1992953/file-operations-in-android-ndk
'Programing' 카테고리의 다른 글
jQuery에서 소수점 이하 2 자리로 숫자를 포맷하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.23 |
---|---|
집합의 모든 하위 집합을 얻는 방법은 무엇입니까? (0) | 2020.11.23 |
단일 값으로 배열 초기화 (0) | 2020.11.23 |
clang-format에서 서식 변경이 필요한지 알려줄 수 있습니까? (0) | 2020.11.22 |
Amazon EC2 키 쌍 복구 (0) | 2020.11.22 |