[英]Azure blob storage from Android
I try to upload image to Azure Blob storage from android.我尝试将图像从 android 上传到 Azure Blob 存储。 I can do it from Java by this way我可以通过这种方式从 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());
But if I change filepath to "content://media/external/images/media/12" this, in android I have FileNotFoundException.但是,如果我将文件路径更改为“content://media/external/images/media/12”,在 android 中我有 FileNotFoundException。 How can I upload images from android?如何从android上传图像?
final String filePath = "C:\\Users\\icon.jpg" final String filePath = "C:\\Users\\icon.jpg"
I pretty much doubt this points to existing file on your device .我非常怀疑这指向您设备上的现有文件。
EDIT编辑
But if I change filepath to content://media/external/images/media/12但是如果我将文件路径更改为 content://media/external/images/media/12
This is NOT file path.这不是文件路径。 Using content://
Uri, requires using a ContentResolver and methods like openInputStream()
and openOutputStream()
.使用content://
Uri,需要使用 ContentResolver 和openInputStream()
和openOutputStream()
。
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();
Convert the stream is like that转换流就是这样
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.