Reputation: 709
I've created a container on Azure called 'assetcontainer'.
I've followed the sample to create the following code:
CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString);
mClient = account.createCloudBlobClient();
mAssetContainer = mClient.getContainerReference("assetcontainer");
// Download the blob
// For each item in the container
for (ListBlobItem blobItem : mAssetContainer.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
try {
retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName()));
}
catch(Exception ex){}
}
}
And I am receiving the following exception when I try to iterate the listBlobs():
Process: com.training.app.debug, PID: 3284
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.training.app.debug/com.training.app.activities.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:783)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:145)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:252)
at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:109)
at com.training.azure.AzureStorage.syncAssets(AzureStorage.java:57)
at com.training.app.activities.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
I'm not sure what could be wrong here... Is this a bug with the azure sdk?
Upvotes: 1
Views: 1248
Reputation: 929
I found a solution from this page, and it worked for me.
Android doesn't allow performing network operations in the main thread, so you need to execute the code in an asynchronous task, like this:
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString);
mClient = account.createCloudBlobClient();
mAssetContainer = mClient.getContainerReference("assetcontainer");
// Download the blob
// For each item in the container
for (ListBlobItem blobItem : mAssetContainer.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
try {
retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName()));
}
catch(Exception ex){}
}
}
}
}
Then you call new MyTask().execute()
in the main thread.
Upvotes: 2