inputstream available()总是在android studio中返回0,同时上传azure blob

时间:2017-08-04 16:52:27

标签: android azure inputstream azure-mobile-services azure-storage-blobs

我试图在天蓝色上传照片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();
    }

问题我已经谷歌关于那个&#34;可用()&#34; InputStream,但我不理解一件事,很多人说有些场合,当数据流被阻塞时,available()会返回0,这会让我感到困惑,什么被阻挡?

有人可以告诉我正确的方法吗?我是Android开发的新手

更新

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

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

1 个答案:

答案 0 :(得分:0)

请确保使用前缀为photoURi的{​​{1}}正确的uri,并尝试以下代码。

file://