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 is=conn.getInputStream();
当我使用parseFile.getData()时,我会得到“无法解码为位图,异常”,这可能是因为检索到的数据不是图像。
当我从浏览器中点击上面的url时,我得到
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>.........</RequestId>
<HostId>
...................
</HostId>
</Error>此错误发生在表中的所有文件中。
请帮助我做错了什么.:(
发布于 2013-07-06 04:40:49
服务器返回403 HTTP错误代码,这意味着访问被拒绝。如果您还没有这样做,您可能需要先进行身份验证。
特别是对于Parse.com,您可以在解析Quickstart中找到有关如何在活动的onCreate()方法中执行Parse.initialize调用的信息。当然,您需要自己的应用程序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;
}https://stackoverflow.com/questions/17497772
复制相似问题