将图片上传到 Flickr 并获得 URL 张图片

Upload image to Flickr and get URL of it

我想在我的聊天应用程序中发送图片消息,所以我打算将它上传到 Flickr 并从中获取静态 url,然后我可以将那个 url 发送到其他设备(也保存到数据库中)。

我知道它的api是:https://up.flickr.com/services/upload/

但是我不知道如何为它设置参数和保存图像。有人有解决方案吗?

这是我的class上传图片任务:

public class UploadPhotoTask extends AsyncTask<OAuth, Void, String> {
    /**
     * 
     */
    private final FlickrjActivity flickrjAndroidSampleActivity;
    private File file;

    // private final Logger logger = LoggerFactory
    // .getLogger(UploadPhotoTask.class);

    public UploadPhotoTask(FlickrjActivity flickrjAndroidSampleActivity,
            File file) {
        this.flickrjAndroidSampleActivity = flickrjAndroidSampleActivity;
        this.file = file;
    }

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

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

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected String doInBackground(OAuth... params) {
        OAuth oauth = params[0];
        OAuthToken token = oauth.getToken();

        try {
            Flickr f = FlickrHelper.getInstance().getFlickrAuthed(
                    token.getOauthToken(), token.getOauthTokenSecret());

            UploadMetaData uploadMetaData = new UploadMetaData();
            uploadMetaData.setTitle("" + file.getName());
            return f.getUploader().upload(file.getName(),
                    new FileInputStream(file), uploadMetaData);
        } catch (Exception e) {
            Log.e("boom!!", "" + e.toString());
            e.printStackTrace();
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }

        if (response != null) {
            Log.e("", "" + response);
        } else {

        }

        if (monUploadDone != null) {
            monUploadDone.onComplete();
        }

        Toast.makeText(flickrjAndroidSampleActivity.getApplicationContext(),
                response, Toast.LENGTH_SHORT).show();

    }

    onUploadDone monUploadDone;

    public void setOnUploadDone(onUploadDone monUploadDone) {
        this.monUploadDone = monUploadDone;
    }

    public interface onUploadDone {
        void onComplete();
    }

}

此外,我们怎样才能得到 URL 个图像?

阅读 developer guide 了解如何使用 api 和工作示例的详细信息。

还有this existing post类似的查询。

尝试按照另一个 post 使用 flickrj library