【发布时间】:2017-08-08 15:00:17
【问题描述】:
我试图在 azure 中上传照片 blob,这是我尝试的方法
private void UploadImage()
{
try {
//photoURI is intent data straight from camera activity
InputStream imageStream = getContentResolver().openInputStream(photoURI);
//this is the problem, the available() is always return zero
int imageLength = imageStream.available();
final Handler handler = new Handler();
Thread th = new Thread(new Runnable() {
public void run() {
try {
final String imageName = ImageManager.UploadImage(imageStream, imageLength);
handler.post(new Runnable() {
public void run() {
Toast.makeText(ReviewnTakePic.this, "Image Uploaded Successfully. Name = " + imageName, Toast.LENGTH_SHORT).show();
}
});
}
catch(Exception ex) {
final String exceptionMessage = ex.getMessage();
handler.post(new Runnable() {
public void run() {
Toast.makeText(ReviewnTakePic.this, exceptionMessage, Toast.LENGTH_SHORT).show();
}
});
}
}});
th.start();
}
catch(Exception ex) {
Toast.makeText(this, "fail to send!", Toast.LENGTH_SHORT).show();
}
}
这是经理
public class ImageManager {
public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"
+"AccountName=blabla;"
+"AccountKey=secret;"
+"EndpointSuffix=core.windows.net";
private static CloudBlobContainer getContainer() throws Exception {
// 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("images");
return container;
}
public static String UploadImage(InputStream image, int imageLength) throws Exception {
CloudBlobContainer container = getContainer();
container.createIfNotExists();
String imageName = randomString(10);
CloudBlockBlob imageBlob = container.getBlockBlobReference(imageName);
imageBlob.upload(image, imageLength);
return imageName;
}
public static String[] ListImages() throws Exception {
CloudBlobContainer container = getContainer();
Iterable<ListBlobItem> blobs = container.listBlobs();
LinkedList<String> blobNames = new LinkedList<>();
for(ListBlobItem blob: blobs) {
blobNames.add(((CloudBlockBlob) blob).getName());
}
return blobNames.toArray(new String[blobNames.size()]);
}
public static void GetImage(String name, OutputStream imageStream, long imageLength) throws Exception {
CloudBlobContainer container = getContainer();
CloudBlockBlob blob = container.getBlockBlobReference(name);
if(blob.exists()){
blob.downloadAttributes();
imageLength = blob.getProperties().getLength();
blob.download(imageStream);
}
}
static final String validChars = "abcdefghijklmnopqrstuvwxyz";
static SecureRandom rnd = new SecureRandom();
static String randomString(int len ){
StringBuilder sb = new StringBuilder( len );
for( int i = 0; i < len; i++ )
sb.append( validChars.charAt( rnd.nextInt(validChars.length()) ) );
return sb.toString();
}
我已经在谷歌上搜索过“available()”InputStream 的问题,但我什么都不懂,很多人说在某些情况下,当数据流被阻塞时,available() 会返回 0,这使得我一头雾水,什么被屏蔽了?
有人能告诉我正确的方法吗?我是 Android 开发新手
更新
老实说,我从Github 中采用了这个方法,如果它使用画廊选择器,所以我将它修改为直接从相机意图中获取 Uri,这是 InputStream 在加载时的内容
content://com.example.android.fileprovider/test/test--05-08-2017--04-58-1026842566.jpg
和原来的项目,内容不同
content://com.android.providers.media.documents/document/image%1234567
我希望我的解释是可以理解的,我看到了区别,但我不知道如何解决这个问题
【问题讨论】:
标签: android azure inputstream azure-mobile-services azure-blob-storage