尝试使用ParseFile getUrl()方法下载图像时获取FileNotFound异常

时间:2013-07-05 22:47:11

标签: android parse-platform

Android平台

我的全局应用程序配置如下

    @Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(this, Application Id, Client key);
        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();       
        defaultACL.setPublicReadAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);
    }

我按如下方式保存了文件

    ParseObject pIssue = new ParseObject(Constants.STUDENT_CLASS);
    pIssue.put(Constants.STUDENT_TITLE, mTitleView.getText().toString());
    if(mCurrentPhotoPath != null){          
        byte[] imgData = photoHelper.convertFileToByteArray(mCurrentPhotoPath);
        ParseFile pFile = new ParseFile("heya",imgData);
        pIssue.put(Constants.STUDENT_MEDIA_FILES, pFile);           
    }       
    pIssue.saveEventually();

convertFileToByteArray方法如下所示

    public byte[] convertFileToByteArray(String filePath) {
        byte[] byteArray = null;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        byteArray = out.toByteArray();
        return byteArray;
    } 

我在一个单独的帖子中检索了图片文件,如下所示

f = new File(filename); // this file is valid 
url=parseFile.getUrl(); // this is the url mentioned below
try {
    Bitmap bitmap=null;
    URL imageUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setInstanceFollowRedirects(true);
    InputStream is=conn.getInputStream(); // code breaks and throws exception here
    OutputStream os = new FileOutputStream(f);
    Utils.CopyStream(is, os);
    os.close();
    conn.disconnect();
    bitmap = decodeFile(f);
    return bitmap;
} catch (Exception ex){
   ex.printStackTrace();
   return null;
}

问题是在检索时遇到以下异常 java.io.FileNotFoundException:http://files.parse.com/e13c8e5c-9234-4160-9d63-b802696f9251/heya

此步骤的代码中断 - InputStream = conn.getInputStream();

当我使用parseFile.getData()时,我得到'无法解码到位图,异常',可能是因为检索到的数据不是图像。

当我从浏览器点击上面的网址时我得到了

<Error>
     <Code>AccessDenied</Code>
     <Message>Access Denied</Message>
     <RequestId>.........</RequestId>

     <HostId>
         ...................
     </HostId>
</Error>

中的所有文件都会出现此错误

请帮助我在哪里出错.............. :(

1 个答案:

答案 0 :(得分:0)

服务器返回403 HTTP错误代码,这意味着访问被拒绝。如果您尚未进行身份验证,则可能需要先进行身份验证。

特别是对于Parse.com,您可以在Parse Quickstart中找到有关如何在您的活动的Parse.initialize方法中进行onCreate()调用的信息。当然,您需要自己的应用程序ID和客户端ID。

通常,最好检查HTTP响应代码,如下所示:

        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);

            int responseCode = conn.getResponseCode();
            if (responseCode >= 300) {
                Log.e("MainActivity", "something went wrong. Response code = " + responseCode);
                return null;
            } else {
                InputStream is = conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                conn.disconnect();
                bitmap = decodeFile(f);
                return bitmap;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }