【问题标题】:How to access image path?如何访问图像路径?
【发布时间】:2013-03-06 21:36:11
【问题描述】:

我正在尝试将照片上传到 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();
}

【问题讨论】:

    标签: java android filenotfoundexception


    【解决方案1】:

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

    请尝试将图片移动到/assets 文件夹并通过getAssets() 访问它,或者将其移动到/res/raw 并通过R.raw.icon 访问它。

    【讨论】:

    【解决方案2】:

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

    打开数据流以读取原始资源。这只能与值为资产文件名称的资源一起使用——也就是说,它可以用于打开可绘制、声音和原始资源;它会在字符串和颜色资源上失败。

    我认为FileBody 构造函数不接受InputStream,因此您可能需要其他方法来发送二进制数据(文件)。

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 1970-01-01
      • 2017-11-21
      • 2014-12-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多