0

安卓平台

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

    @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 is=conn.getInputStream();

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

当我从浏览器点击上述网址时,我得到

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

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

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

请帮助我哪里错了........ :(

4

1 回答 1

0

服务器返回 403 HTTP 错误代码,表示访问被拒绝。如果您还没有这样做,您可能需要先进行身份验证。

专门针对 Parse.com,在Parse 快速入门中,您可以找到有关如何在Activity 方法中进行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;
        }
于 2013-07-06T04:40:49.587 回答