将图像上传到android中的azure blob存储时的nullpointerexception

时间:2014-10-09 11:01:54

标签: java android azure azure-storage-blobs

用户从图库和我在windows azure blob存储上传的所选图像中选择图像。但是在上传时我得到一个空指针异常。

我无法在互联网上找到任何解决方案。 ArrayList<String>&#39;已选中&#39;由所选图像的路径组成。路径在logcat中以这种格式显示:mnt/sdcard/Pictures/image1.jpg

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    selected = new ArrayList<String>();

    try {
        // Retrieve storage account from connection-string.
        storageAccount = CloudStorageAccount.parse(storageConnectionString);

        // Create the blob client.
        blobClient = storageAccount.createCloudBlobClient();

        // Get a reference to a container.
        // The container name must be lower case
        blobContainer = blobClient.getContainerReference("mycontainer");

        // Create the container if it does not exist.
        // Create a blob container using the createIfNotExist method that
        // checks whether a container exists with the same name. The method
        // creates the blob container only if a container with the same name
        // does not exist. Otherwise, no operation is performed. 
        blobContainer.createIfNotExists();

        // Create a permissions object.
        containerPermissions = new BlobContainerPermissions();

        // Include public access in the permissions object.
        containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

        // Set the permissions on the container.
        blobContainer.uploadPermissions(containerPermissions);

    } catch (InvalidKeyException e1) {
        e1.printStackTrace();
    } catch (Exception e1) {
        e1.printStackTrace();
    }             
}

public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

    // Inflate the menu items for use in the action bar

    inflater.inflate(R.menu.mymenu, menu);

    // Here we get the action view we defined
    myActionMenuItem = menu.findItem(R.id.my_action);
    View actionView = myActionMenuItem.getActionView();

    // We then get the button view that is part of the action view
    if(actionView != null) {
        myActionButton = (Button) actionView.findViewById(R.id.action_btn);
        myActionButton.setText(R.string.txt_submit);
        if(myActionButton != null) {
            // We set a listener that will be called when the return/enter key is pressed
            myActionButton.setOnClickListener(new OnClickListener() {                       

                @Override
                public void onClick(View v) {

                    myActionButton.setEnabled(false);

                    myActionButton.setText("Submitting..");                         

                    try {

                        for(int i = 0; i <selected.size();i++){
                            String filePath = selected.get(i).sdcardPath;

                            File source = new File(filePath);
                            String absoluteFilePath = source.getAbsolutePath();
                            Log.d("personal", absoluteFilePath);
                            CloudBlockBlob blob = blobContainer.getBlockBlobReference(source.getName());
                            Log.d("personal", source.getName());
                            //Log.d("personal", imageName.get(i));
                            blob.upload(new FileInputStream(absoluteFilePath), source.length());
                            //blob.uploadFromFile(filePath);
                            Log.d("personal", "Image Uploaded");
                        }

                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });

            }
        }
    }

堆栈跟踪:

10-09 15:50:27.168: W/System.err(1451): java.lang.NullPointerException
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:145)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:252)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.commitBlockList(CloudBlockBlob.java:242)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.BlobOutputStream.commit(BlobOutputStream.java:321)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.BlobOutputStream.close(BlobOutputStream.java:285)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:582)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:499)
10-09 15:50:27.199: W/System.err(1451):     at com.jbandroid.fragment.PersonalInfoFragment$2.onCompleted(PersonalInfoFragment.java:273)
10-09 15:50:27.199: W/System.err(1451):     at com.jbandroid.fragment.PersonalInfoFragment$2.onCompleted(PersonalInfoFragment.java:1)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceTable$ParseResultOperationCallback.onCompleted(MobileServiceTable.java:103)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$2.onCompleted(MobileServiceJsonTable.java:249)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$4.onPostExecute(MobileServiceJsonTable.java:389)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$4.onPostExecute(MobileServiceJsonTable.java:1)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask.finish(AsyncTask.java:602)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-09 15:50:27.199: W/System.err(1451):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:50:27.208: W/System.err(1451):     at android.os.Looper.loop(Looper.java:137)
10-09 15:50:27.208: W/System.err(1451):     at android.app.ActivityThread.main(ActivityThread.java:4340)
10-09 15:50:27.208: W/System.err(1451):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 15:50:27.208: W/System.err(1451):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 15:50:27.208: W/System.err(1451):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-09 15:50:27.208: W/System.err(1451):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-09 15:50:27.208: W/System.err(1451):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

这可能是因为Android不允许主线程上的网络连接。在这种情况下,存储库当前提供了错误的错误消息 - 我们正在努力解决此问题。有关详细信息,请查看其他Stack Overflow post

如果这不是问题,如果您可以提供您正在使用的Android库版本以及您正在运行的Android版本,那将有助于重现问题。