1

我将图像从 android 上传到 aws s3 并从 aws 找到生成的 url 并将其保存在我的服务器上,但一段时间后,当我想使用签名生成的 url 获取图像时,我被拒绝访问。

任何人都可以帮我解决这个问题,我如何通过该 url 从 aws 获取图像?

4

1 回答 1

0

试试这个方法可能对你有帮助

public Bitmap getUrlContent(String urlstring) throws IOException
    {
        byte[] imageRaw = null;
        URL url = new URL(urlstring);

        Authenticator.setDefault(new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                    //your username and password here
                return new PasswordAuthentication(user, password.toCharArray());
            }});
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setUseCaches(false);
        urlConnection.connect();
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            try
            {
                InputStream in = new BufferedInputStream(
                        urlConnection.getInputStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int c;
                while ((c = in.read()) != -1)
                {
                    out.write(c);
                }
                out.flush();

                imageRaw = out.toByteArray();

                urlConnection.disconnect();
                in.close();
                out.close();
                return BitmapFactory.decodeByteArray(imageRaw, 0, imageRaw.length);
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
于 2013-09-05T10:37:51.057 回答