我一直在尝试获取资产文件的URI路径.
uri = Uri.fromFile(new File("//assets/mydemo.txt"));
Run Code Online (Sandbox Code Playgroud)
当我检查文件是否存在时,我看到该文件不存在
File f = new File(filepath);
if (f.exists() == true) {
Log.e(TAG, "Valid :" + filepath);
} else {
Log.e(TAG, "InValid :" + filepath);
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何提及资产文件夹中存在的文件的绝对路径
我的问题是:是否有可能在Android中获取资源的绝对路径(在res /文件夹的子目录中)?
我在谷歌上看到这个问题的大多数答案建议获取文件描述符,暗示这是不可能的.
是吗?
编辑:我想这样做的原因是因为我正在编写接受媒体路径并播放它的类.如果我可以传递资源的绝对路径,那么测试会容易得多.
我一直在使用本教程对图片进行一些人脸检测。问题是当我获取在 java 上使用的文件路径时
String xmlFile = "E:/OpenCV/facedetect/lbpcascade_frontalface.xml";
CascadeClassifier classifier = new CascadeClassifier(xmlFile);
Run Code Online (Sandbox Code Playgroud)
我如何在 android studio 上翻译。我尝试将我的 lbpcascade_frontalface.xml 放在原始资源上。CascadeClassifier 是 opencv 库提供的一个类。唯一的问题是它们只加载了字符串路径(在 xmlfile 上)。这是我的代码。
String pathtoRes = getRawPathAtempt2(context);
CascadeClassifier cascadeClassifier = new CascadeClassifier();
cascadeClassifier.load(pathtoRes);
Run Code Online (Sandbox Code Playgroud)
我翻译成这样的方法。
public String getRawPathAtempt2(Context context) {
return "android.resource://" + context.getPackageName() + "/raw/" + "lbpcascade_frontalface.xml";
}
Run Code Online (Sandbox Code Playgroud)
我收到 opencv 的断言错误,告诉我文件为空。那意味着当我在我的方法中使用文件路径时我错了。如何获取原始资源的文件路径?请帮帮我我已经被困了好几天了
你好,我需要提供我的应用程序原始文件夹中的 pdf 文件的路径,即(R.raw.input.pdf)
请查看我的主要 java 文件,并请帮我提供原始文件夹的路径
public class MainBrowserActivity extends Activity {
private Button btnPDF;
public Scanner scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPDF = (Button) findViewById(R.id.btnOpenPDF);
btnPDF.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Resources res = getResources();
scan = new Scanner(res.openRawResource(R.raw.input));
String uri = "android.resource://" + getPackageName() + "/"+R.raw.input;
System.out.println("FIle Path is" + uri);
final Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(uri), "pdf/*");
intent.setClass(MainBrowserActivity.this,PdfViewerActivity.class);
startActivity(intent);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)