Вопрос

I am using the new file chooser of Android to let the user open my files. I am trying to select a private key file (*.key, pem formatted):

    Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    [...]
    construct supportedMimeTypesArray as array with
    "application/x-pem-file"
    "application/pkcs8"
    [...]
    i.putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypesArray);

This works when choosing files from the internal storage. But when I try to open a file from Google, Google drive blanks out the file.

The files were uploaded using a browser.

Is there any way to show what mime type Google drive thinks the files are?

Это было полезно?

Решение

The way to go is to request the file with a mime type of "/" and then and let show yourself the meta fields of the returned URI. As it turns out a ".key" file has a mime-type of "application/x-iwork-keynote-sffkey"

A quick&dirty method is given here:

    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 Intent resultData) {
        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            Uri uri = null;
            if (resultData != null) {
                uri = resultData.getData();
                Log.i(TAG, "Uri: " + uri.toString());

                Cursor c = getActivity().getContentResolver().query(uri, null, null, null, null);
                try {
                    String r = "";
                    c.moveToFirst();
                    do {
                        for (int i = 0; i < c.getColumnCount(); i++) {
                            r += c.getColumnName(i) + "      " + c.getString(i) + "\n";

                        }
                        r += "\n";
                    } while (c.moveToNext());
                    mResultView.setText(r);
                } finally {
                    c.close();
                }

            }
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top