当前位置: 首页 > 知识库问答 >
问题:

android:如何使用Flickrj-Android-2.0.0 android库在flickr上传照片

扈德容
2023-03-14

我使用Flickrj-Android-2.0.0,并遵循OAuth流程。我也得到了访问令牌和秘密,但当我试图在flickr上传照片时,我得到了这个错误:

public void uploadPhoto(OAuth... params)
            throws ParserConfigurationException {
        OAuth oauth = params[0];
        OAuthToken token = oauth.getToken();
        RequestContext requestContext = RequestContext.getRequestContext();
        OAuth auth = new OAuth();
        auth.setToken(new OAuthToken(token.getOauthToken(), token
                .getOauthTokenSecret()));
        requestContext.setOAuth(auth);
        Uploader up = new Uploader(FlickrHelper.API_KEY, FlickrHelper.API_SEC);
        UploadMetaData uploadMetaData = new UploadMetaData();
        uploadMetaData.setTitle("hello world");
        try {
            Drawable d = getResources().getDrawable(R.drawable.icon);
            // the drawable (Captain Obvious, to the rescue!!!)
            Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitmapdata = stream.toByteArray();
            System.out.println("Bitmap value= " + bitmapdata);
            userIcon.setImageBitmap(bitmap);
            up.upload("Hello From Emulator", bitmapdata, uploadMetaData);

        } catch (FlickrException e) {

            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

谢谢...

共有1个答案

狄溪叠
2023-03-14

我已经通过更改OAuthTask类来解决...问题是只有许可...将permission.write替换为permission.read

public class OAuthTask extends AsyncTask<Void, Integer, String> {

// private static final Logger logger = LoggerFactory
// .getLogger(OAuthTask.class);
private static final Uri OAUTH_CALLBACK_URI = Uri
        .parse(FlickrjAndroidSampleActivity.CALLBACK_SCHEME + "://oauth"); //$NON-NLS-1$

/**
 * The context.
 */
private Context mContext;

/**
 * The progress dialog before going to the browser.
 */
private ProgressDialog mProgressDialog;

/**
 * Constructor.
 * 
 * @param context
 */
public OAuthTask(Context context) {
    super();
    this.mContext = context;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog = ProgressDialog.show(mContext,
            "", "Generating the authorization request..."); //$NON-NLS-1$ //$NON-NLS-2$
    mProgressDialog.setCanceledOnTouchOutside(true);
    mProgressDialog.setCancelable(true);
    mProgressDialog.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dlg) {
            OAuthTask.this.cancel(true);
        }
    });
}

/*
 * (non-Javadoc)
 * 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected String doInBackground(Void... params) {
    try {
        Flickr f = FlickrHelper.getInstance().getFlickr();
        OAuthToken oauthToken = f.getOAuthInterface().getRequestToken(
                OAUTH_CALLBACK_URI.toString());
        saveTokenSecrent(oauthToken.getOauthTokenSecret());
        URL oauthUrl = f.getOAuthInterface().buildAuthenticationUrl(
                Permission.WRITE, oauthToken);
        return oauthUrl.toString();
    } catch (Exception e) {
        //          logger.error("Error to oauth", e); //$NON-NLS-1$
        return "error:" + e.getMessage(); //$NON-NLS-1$
    }
}

/**
 * Saves the oauth token secrent.
 * 
 * @param tokenSecret
 */
private void saveTokenSecrent(String tokenSecret) {
    //      logger.debug("request token: " + tokenSecret); //$NON-NLS-1$
    FlickrjAndroidSampleActivity act = (FlickrjAndroidSampleActivity) mContext;
    act.saveOAuthToken(null, null, null, tokenSecret);
    //      logger.debug("oauth token secret saved: {}", tokenSecret); //$NON-NLS-1$
}

@Override
protected void onPostExecute(String result) {
    if (mProgressDialog != null) {
        mProgressDialog.dismiss();
    }
    if (result != null && !result.startsWith("error")) { //$NON-NLS-1$
        mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                .parse(result)));
    } else {
        Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
    }
}

}

查看完整演示

 类似资料:
  • 我尝试了DPZFlickr,它在尝试上传时返回以下内容: 检查Flickr API的响应,它与尝试Dantsu版本的phpFlickr时返回的结果相同,返回如下: 我卷了自己的卷曲,返回如下:

  • 我需要将Flickr与Android集成。我完成了认证。我需要上传图片到flickr,但我不知道如何做同样。我参考文档:http://www.flickr.com/services/api/upload.api.html。有人能帮我吗

  • 问题内容: 我想使用Flickr Java API(flickrj)在Flickr中下载特定用户的照片集 有人知道怎么做吗? 我已经用以下代码声明了: 接下来我该怎么办? 问题答案: 我找到了答案,然后有人可以帮助我。 这是代码:

  • 我收到代码为200的响应,但图像没有上传到服务器

  • 我已经得到了Flickr照片的id,我能找到照片对应的组id、集合id和它所属的池id吗?