来自Android的Azure blob存储

问题描述 投票:-1回答:2

我尝试从android上传图像到Azure Blob存储。我可以通过这种方式从Java做到这一点

 CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

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

        CloudBlobContainer container = blobClient.getContainerReference("mycontainer");

        final String filePath = "C:\\Users\\icon.jpg";

        CloudBlockBlob blob = container.getBlockBlobReference("1.jpg");
        File source = new File(filePath);
        blob.upload(new FileInputStream(source), source.length());

但是,如果我将文件路径更改为“content:// media / external / images / media / 12”,在android中我有FileNotFoundException。我怎样才能从android上传图片?

java android image azure blob
2个回答
0
投票
            CloudBlockBlob blob = container.getBlockBlobReference(UserInfo.username + ".jpg");
            BlobOutputStream blobOutputStream = blob.openOutputStream();
            ContentResolver cr = context.getContentResolver();
            InputStream s = cr.openInputStream(uri);
            byte[] arr = convertInputStreamToByteArray(s);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(arr);
            int next = inputStream.read();
            while (next != -1) {
                blobOutputStream.write(next);
                next = inputStream.read();
            }
            blobOutputStream.close();

转换流就是这样

 public byte[] convertInputStreamToByteArray(InputStream inputStream)
    {
        byte[] bytes= null;

        try
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte data[] = new byte[1024];
            int count;

            while ((count = inputStream.read(data)) != -1)
            {
                bos.write(data, 0, count);
            }

            bos.flush();
            bos.close();
            inputStream.close();

            bytes = bos.toByteArray();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return bytes;
    }

1
投票

final String filePath =“C:\ Users \ icon.jpg”

我非常怀疑这指向您设备上的现有文件。

编辑

但是,如果我将文件路径更改为content:// media / external / images / media / 12

这不是文件路径。使用content:// Uri,需要使用ContentResolver和openInputStream()openOutputStream()等方法。

© www.soinside.com 2019 - 2024. All rights reserved.