我在使用最近发布的 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));
很难诊断,因为我不知道卡在内部做什么。