2013-03-06 73 views
0

我试图上传照片到Flickr,但这段代码抛出了一个文件的异常未找到。图像在文件夹中,但我不知道为什么Android会抛出异常。请帮忙。如何访问图像路径?

OAuthRequest request = new OAuthRequest(Verb.POST, 
     "http://api.flickr.com/services/upload/"); 
service.signRequest(accessToken, request); 
MultipartEntity entity = new MultipartEntity(); 
entity.addPart("photo", new FileBody(new File("/test/res/drawable-hdpi/icon.png"), 
     "image/png")); 
ByteArrayOutputStream out = new ByteArrayOutputStream(); 
try { 
    entity.writeTo(out); 
    request.addPayload(out.toByteArray()); 
    request.addHeader(entity.getContentType().getName(), 
      entity.getContentType().getValue()); 

    Response response = request.send(); 
    String body = response.getBody(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

回答

0

您无法通过它们的绝对路径访问位于drawable文件夹中的文件。

而是尝试在/assets文件夹移动画面和由getAssets()访问或R.raw.icon移动它在/res/raw和访问它。

+0

R.raw.icon不工作 – 2013-03-06 21:47:54

+0

看一看[这里](http://stackoverflow.com/questions/4081763/access-resource-files-in-android) – niculare 2013-03-06 21:53:59

0

尝试用openRawResource(int id)打开你的文件:

用于读取原始资源打开一个数据流。这只能用于其资源文件名称的资源 - 也就是说,它可以用于打开可绘制,可靠和原始资源;它将在字符串和颜色资源上失败。

我觉得FileBody构造函数不接受InputStream,所以你可能需要其他的方法来发送二进制数据(文件)。