我正在尝试制作一个在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语句)。我首先尝试将文件夹放在我的手机sd卡中,之后放在我的内部存储中,但无论如何都不起作用。我在手机上检查了一下,我想路径是正确的:
链接到显示文件夹路径的图像 此外,我在Android清单中拥有读写外部存储权限。
您知道问题出在哪里或者是什么问题吗?
非常感谢!
编辑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在该路径下: