简体   繁体   English

在资产文件夹中找不到文件或资源

[英]not found as file or resource in assets folder

I have a question, why can't access a file in my assets folder? 我有一个问题,为什么不能访问我的资产文件夹中的文件?

Folder

在此处输入图片说明

My code 我的密码

Uri path = Uri.parse("android.resource://com.hackro.tutorials.myapplication/raw/comprobante.pdf");

String newPath = path.toString();

Resources res = getResources();

try {

    PdfReader reader = new PdfReader(newPath);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/sdcard/evidencia.pdf"));
    AcroFields fields = stamper.getAcroFields();

    fields.setField("Referencia", referencia);

    fields.setField("Fecha y Hora", fechahora);
    fields.setField("Tipo", tipo);
    fields.setField("Operacion", operacion);
    fields.setField("no. tarjeta", tarjeta);
    fields.setField("vencimiento", vencimiento);
    fields.setField("monto", monto);
    fields.setField("concepto", concepto);
    fields.setField("Nombre", nombre);
    fields.setField("Autorizacion", autorizacion);


    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
} catch (Exception e) {
    Log.e("error:   ", e.getMessage());
}

Exception 例外

java.io.IOException: android.resource://com.hackro.tutorials.myapplication/raw/comprobante.pdf not found as file or resource.

You have a few problems here. 您在这里遇到一些问题。

First, resources are not assets. 首先,资源不是资产。 You are attempting to use the android.resource scheme to access an asset as if it were a raw resource, and this will not work. 您正在尝试使用android.resource方案来访问资产,就好像它是原始资源一样,这将无法正常工作。

Second, you are constructing a Uri , then are converting that to a string, then are trying to wrap that in a File object. 其次,您要构造一个Uri ,然后将其转换为字符串,然后尝试将其包装在File对象中。 That will never work, for any type of Uri . 对于任何类型的Uri ,这都是行不通的。

Third, neither resources nor assets are files on the filesystem of the device. 第三,资源和资产都不是设备文件系统上的文件。 They are files on the filesystem of your development machine. 它们是开发计算机文件系统上的文件。 They are entries in the APK file on the device. 它们是设备上APK文件中的条目。 You cannot get a File object pointing to a resource or an asset. 您无法获得指向资源或资产的File对象。

Use getAssets() on a Context (eg, your Activity or Service ) to get an AssetManager . Context (例如,您的ActivityService getAssets()上使用getAssets()获取AssetManager Then, call open("comprobante.pdf") on that AssetManager to get an InputStream on the asset. 然后,在该AssetManager上调用open("comprobante.pdf")以获取该资产的InputStream Pass that to the PdfReader constructor and hope that your PDF library supports an InputStream . 将其传递给PdfReader构造函数,并希望您的PDF库支持InputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM