-1

I am successfully loading and using an PKCS12 file Cert.pfx from Assets like this:

String certLocation = "certificates/Cert.pfx";
InputStream isCert = null;
try {
    isCert = getAssets().open(certLocation);
} catch (Exception e) {
    Log.d(TAG, "Could not get Assets");
    }

// Work with the certificate

I am trying to do the same now, but reading from a storage in Android.

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/PrintableRSA");
File cert = new File(dir,"Cert.pfx");

InputStream isCert= null;

try {
    isCert = new BufferedInputStream(new FileInputStream(cert));
    isCert.close();
    Log.d(TAG,"Sucsess!");
} catch (Exception e) {
    Log.d(TAG, "Could not load the file");
}

   // Same work with the certificate

The file loads sucsessfully, but When I further to work with isCert InputStream, loaded from storage, it does not work. I suspect that therefore the files are not equal.

I also tried with InputStream isCert = FileInputStream(cert); unsuccessfully.

How can I fix this issue?

cvetozaver
  • 193
  • 3
  • 9

1 Answers1

1

You clearly cannot work with the InputStream at the point you labelled "Same work with the certificate" since you have already called close() on it by that point.

Move your call to close() to after you have finished with the stream.

zmarties
  • 4,635
  • 18
  • 38
  • I checked my original code. I had an `this.myCert = isCert` between the `isCert` declaration and `isCert.close()` statement. I wrongfully thought that the InputStream gets copied at that point. I guess when the `isCert` is closed, the `this.myCert` becomes `null` as well. Can you confirm this? – cvetozaver Aug 26 '14 at 16:03