![](/img/trans.png)
[英]Copy file to photo library using C++ / NDK (Android Development)
[英]Open and read text file in c++ with Android (ndk)
我正在尝试在Android中制作一个使用C ++中的OpenCV的应用程序。 我面临的问题是我想从本机部分打开文本文件以逐字读取它,但无法打开。 我的代码如下:
JNI方法:
JNIEXPORT jboolean JNICALL Java_com_alejandro_nativeopencv_NativeClass_initRecognizer(JNIEnv * env, jclass clazz){
recognizer = Recognizer("ORB","ORB","BruteForce-Hamming");
recognizer.createObject("/storage/emulated/0/TFG/Fotos/Carpeta", true);
recognizer.createObject("/storage/emulated/0/TFG/Fotos/Cereales", true);
return true;
}
和我正在使用的Recognizer :: createObject方法:
Recognizer.cpp :
#include "headers/Recognizer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <android/log.h>
using namespace std;
using namespace cv;
//...other methods, constructors and stuff...
Object Recognizer::createObject(String path, bool add) {
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
String name;
vector < vector<KeyPoint> > keypoints;
vector <Mat> descriptors;
vector < vector<Point2f> > corners;
vector <String> viewNames;
bool easy;
string word;
int i = 0;
while (file >> word)
{
if(i == 0){
/* Object name */
name = word;
__android_log_print(ANDROID_LOG_DEBUG,"NOMBRE","%s",name.c_str());
} else if(i == 1){
/* Object easy or not */
easy = (word == "true");
__android_log_print(ANDROID_LOG_DEBUG, "EASY", "%d", easy);
} else if(i%2 == 0){
/* Object image view*/
keypoints.push_back(vector <KeyPoint>());
descriptors.push_back(Mat());
corners.push_back(vector <Point2f>(4));
Mat image = imread(path + "/" + word, CV_LOAD_IMAGE_GRAYSCALE);
this->detector->detect(image, keypoints[(i/2)-1]);
this->extractor->compute(image, keypoints[(i/2)-1], descriptors[(i/2)-1]);
corners[(i/2)-1][0] = cvPoint(0,0);
corners[(i/2)-1][1] = cvPoint(image.cols,0);
corners[(i/2)-1][2] = cvPoint(image.cols, image.rows);
corners[(i/2)-1][3] = cvPoint(0, image.rows);
__android_log_print(ANDROID_LOG_DEBUG, "VISTA", "%d", (i/2)-1);
} else{
/* Object name view */
viewNames.push_back(word);
String aux = word;
__android_log_print(ANDROID_LOG_DEBUG, "VISTA NOMBRE", "%s", aux.c_str());
}
i++;
}
Object obj = Object(name, keypoints, descriptors, corners, viewNames, easy);
if(add){
this->objects.push_back(obj);
}
file.close();
return obj;
}
}
问题在这里:
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
...do stuff...
}
在JNI方法中,我将要读取的info.txt文件所在的路径作为参数传递,但没有打开它(没有错误,它只输入第一个if语句。我首先尝试将我的手机sdcard中的文件夹,然后在内部存储中,但是它不能以任何方式工作。我检入了手机,我猜路径是正确的:
另外,我在Android Manifest中具有读写外部存储权限。
您知道这里的问题是什么?
非常感谢你!
编辑1:
将代码更改为此:
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file;
file.open((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
...do stuff...
}
仍然有同样的问题。
编辑2
我得到的日志结果是这些(它们与它尝试访问的路径相对应):
D/path: /storage/emulated/0/TFG/Fotos/Carpeta/info.txt
D/path: /storage/emulated/0/TFG/Fotos/Cereales/info.txt
根据我的设备,文件info.txt在该路径中:
该file
尚未打开,您必须在is_open
检查之前调用file.open()
函数将流与当前文件关联。
file.open();
if (!file.is_open()){
return Object(true);
}
更新:另一个原因可能是您没有运行时权限模型。 尝试官方文档和此链接以获取准确的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.