Android 使用文件URI设置Google Glass卡图像失败

Android 使用文件URI设置Google Glass卡图像失败,android,google-glass,google-gdk,Android,Google Glass,Google Gdk,我在使用最近发布的GDK卡时遇到问题。基本上,Card.addImage()只能接受两个参数,一个资源id或一个URI 对于我的用例,我需要打开一个作为文件而不是直接作为资源存在的图像。因此,出于测试目的,我将图像包括在assets文件夹中。尝试直接从资产文件夹访问它们失败,因此我将它们从那里复制到内部存储。一旦它们被复制,我从文件中生成一个URI并将其分配给卡。生成的卡在图像应该显示的位置显示一个灰色块 String fileName = step.attachment; //of the f

我在使用最近发布的GDK卡时遇到问题。基本上,Card.addImage()只能接受两个参数,一个资源id或一个URI

对于我的用例,我需要打开一个作为文件而不是直接作为资源存在的图像。因此,出于测试目的,我将图像包括在assets文件夹中。尝试直接从资产文件夹访问它们失败,因此我将它们从那里复制到内部存储。一旦它们被复制,我从文件中生成一个URI并将其分配给卡。生成的卡在图像应该显示的位置显示一个灰色块

String fileName = step.attachment; //of the form, "folder1/images/image1.jpg"
File outFile = new File(getFilesDir()+File.separator+fileName); 
FileChannel inputChannel = null;
FileChannel outputChannel = null;

try {
    //check to see if the file has already been cached in internal storage before copying
    if(!outFile.exists()) {
        FileInputStream inputStream = new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
        FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
        inputChannel = inputStream.getChannel();
        outputChannel = outputStream.getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

    }

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
finally{
try {
    if(inputChannel!=null)
        inputChannel.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    if(outputChannel!=null)
        outputChannel.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}
card.addImage(Uri.fromFile(outFile));
很难诊断,因为我不知道卡内部在做什么。

而不是写

new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
你能试试吗

getAssets().openFd(fileName).createInputStream();
看看它是否有效


为了回答您最初的问题,
addImage
方法支持
资源:
文件:
uri。

这很奇怪,但我设法解决了我的问题。我用以下代码替换了文件副本代码,它似乎解决了我的问题

InputStream in = null;
OutputStream out = null;
try {
  in = getAssets().open(step.attachment);
  out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
  in.close();
  in = null;
  out.flush();
  out.close();
  out = null;
} catch(IOException e) {
    Log.e("tag", "Failed to copy asset file: " + step.attachment, e);
}  
我不清楚为什么/如何复制我的整个apk,但我猜这是对

getAssets().openFd(fileName).getFileDescriptor()

也许它正在返回apk的文件描述符。这很奇怪,因为我看到一些人声称前面的方法是有效的。

好吧,这让我大吃一惊。我将图像的输出目录更改为/sdcard/,以便查看图像是否被正确复制。它们都是巨大的,大小都一样,103MB。巧合的是,它与我的APK大小相同(为了测试目的,我正在将大量内容打包到其中)。我用7zip打开了文件,这是我的apk!这是怎么回事?我是怎么复制我的申请表的?我在看到你的建议之前就试过了。这似乎起到了作用,不过我觉得奇怪的是,获取资产的文件描述符会返回apk的文件描述符。事实上,我在这里看到了复制文件的方式,这是一种建议的方法: