【问题标题】:Azure Android: Uploading a Photo to BLOB Storage errorAzure Android:将照片上传到 BLOB 存储错误
【发布时间】:2015-05-18 05:56:46
【问题描述】:

我正在尝试将我的 android 设备中的选定内容上传到我的 Azure Blob 存储。首先,我从我的图库中选择一张图片,然后将图片上传到我的云存储。但它会抛出这个错误:

java.io.FileNotFoundException: /content:/media/external/images/media/126310: open failed: ENOENT (No such file or directory)

这是我的代码:

 Button choosePhoto = (Button) (findViewById(R.id.choosePhotoBtn));
            choosePhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
                }
    });   

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {

            if (requestCode == 1) {

                Log.d("Testing", "Uploading");

                // currImageURI is the global variable I'm using to hold the content:// URI of the image
                currImageURI = data.getData();
                TextView tv = (TextView) findViewById(R.id.photoPath);
                tv.setText(currImageURI.toString());

                new MyTask().execute();
            }
        }
    }



    private class MyTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            //Here starts the code for Azure Storage Blob
            try{
                // Retrieve storage account from connection-string
                CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

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

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

                // Create the container if it does not exist
                container.createIfNotExists();

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

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

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

                // Create or overwrite the "myimage.jpg" blob with contents from a local file
                CloudBlockBlob blob = container.getBlockBlobReference("image_1055026155.jpg");
                File source = new File(currImageURI.toString());
                blob.upload(new FileInputStream(source.getAbsolutePath()), source.length());
                Log.d("Testing", "Done");
            }
            catch(Exception e){
                Log.e("SARATH", e.toString());
            }
            return null;
        }
    }

好像找不到我的文件?大家觉得哪里不对?

【问题讨论】:

  • 根据错误消息,我认为您尝试上传的文件不存在,或者可能只是在您要查找的目录中不存在。
  • @EmilyGerner-Microsoft 它存在。我尝试了其他 10 张图片,他们也这样做了。您有可能使用过的代码示例吗?
  • 不幸的是,我提供的一个样本只会显示我使用我的文件系统,而这对你的文件系统并没有真正的帮助。有关下一步尝试的一些想法,请参阅下面的答案。

标签: android azure azure-storage azure-mobile-services azure-blob-storage


【解决方案1】:

我建议通过在 File source = new File(...) 行之后添加几行来检查是否存在(存在),然后尝试创建该文件(createNewFile)来进行调试。有关更多信息,请参阅File API docs。然后,您可以看到新文件的创建位置,这将使您了解文件系统的实际位置。

另外,虽然提供 FileInputStream 的代码是有效的,但使用存储库中内置的 uploadFromFile 方法可能更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 2018-02-26
    • 2023-03-28
    • 2019-05-26
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多