0

I am adding the SSL keystore in the android project and for connecting via SSH, I would like to access the keystore file as a Jetty resource or a File(which I can convert to resource). Unfortunately, I am new to Android and don't know how to access raw resources in such manner. Any help would be nice. Thank you.

Code :

    import org.eclipse.jetty.util.resource.Resource;

    performConnection(){
// The file is present in raw directory. 
Resource keystore = Resource.newClassPathResource("raw/domain.keystore");
    }

Also, as you can in the screenshot, there are multiple parameters Resource class takes. Anyone is suitable as long as I can access the keystore as that task is only proving taxing.

Screenshot :

enter image description here

Any help would be nice. Thank you.

We are Borg
  • 5,117
  • 17
  • 102
  • 225

3 Answers3

1

I am new to Android and don't know how to access raw resources in such manner

Raw resources are files on your development machine. They are not files on the device.

See if you can provide an InputStream. If you can, use getResources().openRawResource() to get an InputStream on your raw resource.

Otherwise, you will have to use that InputStream to copy the raw resource to a local file, such as on internal storage, so you have a File to use.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Do you mean the files in res/raw/ are not on Android phone, I don't get that. Unfortunately that's the first thing I tried to access the file as InputStream, which is stated here how to do : http://stackoverflow.com/questions/15912825/how-to-read-file-from-res-raw-by-name , but nothing. Is there any where else I can put the file in the project and access it easily... – We are Borg Apr 20 '16 at 15:35
  • @WeareBorg: "Do you mean the files in res/raw/ are not on Android phone, I don't get that" -- most of what goes into your APK file does not turn into a file that you can access on the device. Mostly, it just stays in the APK file. "which is stated here how to do" -- it would be far easier to just use `openRawResource(R.raw.whatever_you_called_it)`. "Is there any where else I can put the file in the project and access it easily" -- you are welcome to put it in `assets/` and get an `InputStream` via an `AssetManager`. – CommonsWare Apr 20 '16 at 15:40
0
private Resource loadKeystoreFromAPK(int rId) throws IOException {
        File baseDir = getApplication().getExternalFilesDir(null);
        File path = new File(baseDir, "keystore");
        // Check if it exists, if not, create it
        if (!path.exists()) {
            path.mkdir();
        }
        path = new File(path, "truststore.store");
        if(!path.exists()){
            path.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(path);
        InputStream in = getResources().openRawResource(rId);
        IOUtils.copy(in,out);
        in.close();
        out.close();
        return Resource.newResource(path);
    }
Valitos
  • 36
  • 3
0

I did not get any of these suggestions to work when I found my way here trying to solve this problem of retrieving my keystore in my android app. It could not find the keystore in the raw folder when I tried the previously suggested solutions.

This is how I solved it:

  1. I created a folder called assets in my main folder app/src/main/assets where I put my keystore file.

  2. Then I could use these steps to retrieve my Keystore called keystore.p12.

    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    AssetManager ass = context.getAssets();
    keyStore.load(ass.open("keystore.p12"), keyStorePassword.toCharArray());

It took me some time to figure this out so I hope this can help someone else. You will need to surround it by a try/catch block as well to handle possible exceptions. If you do this in an activity you can call getAssets() without the context part.

dan1st
  • 12,568
  • 8
  • 34
  • 67
Vanheden
  • 582
  • 5
  • 14