1

I'm having issues using the Cards from the recently released GDK. Basically, Card.addImage() can only take two arguments, a resource id or a URI.

For my use case, I need to open an image that exists as a file not directly as a resource. So for testing purposes I'm including the images in the assets folder. Trying to access them directly from the asset folder fails, so I'm copying them from there to internal storage. Once they're copied, I generate a URI from the file and assign it to the card. The resulting card shows a grey block where the image should be.

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));

It's hard to diagnose because I have no clue what the Card is doing internally.

csrussell
  • 11
  • 3
  • Ok, this is blowing my mind. I changed the output directory of the images to /sdcard/ so that I could see if the images were being correctly copied. They were all humongous, and all the same size, 103MB. That coincidentally is the same size as my APK (I'm packing a lot of content into it for test purposes). I opened up the file in 7zip, and it is my apk! What's going on here? How did I copy my application? – csrussell Nov 27 '13 at 21:20

2 Answers2

0

Instead of writing

new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());

can you try

getAssets().openFd(fileName).createInputStream();

and see if it works?

To answer your original question, the addImage method supports resource: and file: URIs.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • I actually tried this before seeing your suggestion. This seems to do the trick, although I find it curious that getting the file descriptor of an asset returns the file descriptor of the apk. In fact I had seen the way I was copying the files as a suggested method here: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard – csrussell Nov 28 '13 at 16:18
0

This is very strange, but I managed to solve my problem. I replaced the file copy code with the following and it appears to have solved my issues

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);
}  

It's not clear to me why/how I was copying my entire apk, but I'm guessing it's the call to

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

Perhaps it was returning the file descriptor of the apk. It's odd because I've seen some claim that the previous method works.

csrussell
  • 11
  • 3