简体   繁体   中英

Android: How to upload photo on flickr using flickrj-android-2.0.0 android library

I an using flickrj-android-2.0.0 and follow the OAuth flow. I am getting the access-token and secret also but when I try to upload the photo on flickr I am getting the this error:

com.googlecode.flickrjandroid.FlickrException: 99: Insufficient permissions. Method requires write privileges; read granted .

Even I have changed the permission to write when I am creating my api-key and secret, but still getting the same error.

For uploading photo I am using the below code please help me to solve this out I am really stuck at this part.

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();
        }
    }

Thanks...

I have solved by changing OAuthTask class... The problem was the permission only... Put Permission.WRITE instead of 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();
    }
}

}

see Full Demo

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM