java - 将图像上传到 android 中的 azure blob 存储时出现 nullpointerException

标签 java android azure azure-blob-storage

用户从图库中选择图像以及我在 Windows Azure Blob 存储上上传的所选图像。但是在上传时我遇到了空指针异常。

我在互联网上找不到任何解决方案。 ArrayList<String> “selected”由所选图像的路径组成。路径在 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)

最佳答案

这可能是因为 Android 不允许在主线程上进行网络连接。在这种情况下,存储库当前提供了不正确的错误消息 - 我们正在努力修复此问题。看看这个其他Stack Overflow post了解更多信息。

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

关于java - 将图像上传到 android 中的 azure blob 存储时出现 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276754/

相关文章:

android - Firebase:获取对第一个项目/元素的引用

android - 如何修复 mitmproxy 不显示应用程序流量

azure - Visual Studio 2019 无法更新 Azure Web 应用中的 wwwroot 文件夹内容

Azure数据工厂: Handling inner failure in until/for activity

azure - 如何从 Microsoft Azure AD 登录过程获取用户主体名称

java - 如何将avd中sd卡中的图像存储到android中的数据库中?

java - BlockingQueue 是否有最大阻塞时间

java - 无法访问我的 WebContent 文件夹中的 index.html

java - 在数组列表中找不到 "."符号

Android,如何知道我的 View 被软键盘adjustPan移动了​​多少