這裏有您需要使用that example,使其在Android的功能後作出改變。
- 從Google Console下載private_key.p12文件並放入資產。
- 保存cloudstorage.properties文件中的資產。
現在讓這些方法更改爲CloudStorage
類。
private Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
AssetManager manager = context.getAssets();
InputStream stream = manager.open("cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException
e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
if (stream != null)
stream.close();
}
}
return properties;
}
private Storage getStorage() throws Exception {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
}
return storage;
}
private File getPrivateKeyFile() {
File f = new File(context.getCacheDir() + 「/my_private_key.p12");
if (!f.exists())
try {
InputStream is = context.getAssets().open(「private_key.p12");
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = is.read(buffer)) != -1)
fos.write(buffer, 0, read);
fos.flush();
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("FILE", "FETCHED FILE:: " + f.getAbsolutePath() + " with data: " + f.length());
return f;
}
嘿,你知道如何從雲存儲中獲取直接圖像URL,以列表視圖顯示圖像嗎? – 2014-09-10 04:26:50
@LOG_TAG我沒有實現了,但是我建議你使用這個[庫](https://github.com/pliablematter/simple-cloud-storage)。它很好地解釋了,是的,你可以從你的雲存儲獲得文件列表。希望對你有用 – monchoz 2014-09-10 05:33:08
我不確定它是否可以從GCS文件獲取公共URL,但有一種方法應該是將文件下載到內部存儲,然後以列表視圖顯示它們。 – monchoz 2014-09-10 05:42:09