inputstream available() 在上传 azure blob 时总是 return 0 in android studio

inputstream available() always return 0 in android studio while uploading azure blob

我试图在 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();
    }

我已经 google 关于那个 "available()" InputStream 的问题,但是我一点都不明白,很多人说在某些情况下 available() 会 return 0 数据流被阻塞时,搞得我一头雾水,阻塞的是什么?

有人能告诉我正确的方法吗?我是 Android 开发部

的新手

更新

老实说,我从 Github 中采用了这种方法,在这种情况下它使用了图库选择器,所以我修改了它以直接从 Camera intent 中获取 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

我希望我的解释是可以理解的,我看到了不同之处,但我不知道如何解决这个问题

请确保使用前缀为 file://photoURi 的正确 uri,然后尝试下面的代码。

long size = 0L;
File file = new File(photoURI);
if(file.exists()) {
    FileInputStream fis = new FileInputStream(file);
    size = fis.available();
    fis.close();
}