Uploading Images to tumblr API from Android

半城伤御伤魂 提交于 2019-12-03 05:08:45

Why don't you use Jumblr the official Java client for Tumblr.

Regards.

You can easily do this using jumblr - Tumblr java client

JumblrClient client = new JumblrClient(Constant.CONSUMER_KEY,Constant.CONSUMER_SECRET);

client.setToken(preferences.getString("token",null), preferences.getString("token_secret", null));

PhotoPost pp = client.newPost(client.user().getBlogs().get(0).getName(),PhotoPost.class);

pp.setCaption(caption);
// pp.setLinkUrl(link);
// pp.setSource(mImage); // String URL
pp.setPhoto(new Photo(imgFile));
pp.save();
silentsudo

This worked for me...

nameValuePairs.add(new BasicNameValuePair(URLEncoder
                .encode("type", "UTF-8"),
                     URLEncoder.encode("photo", "UTF-8")));
Log.e("Tumblr", "Image shareing file path" + filePath);
nameValuePairs.add(new BasicNameValuePair("caption", caption));
nameValuePairs.add(new BasicNameValuePair("source", filePath));`

where filePath is http url.

I have use multipart public class VideoUploader extends AsyncTask {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progressDialog = ProgressDialog.show(RecordingActivity.this, "",
                "Uploading video.. ");
        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject jsonObject = null;
        StringBuilder builder = new StringBuilder();
        try {
            String url = UrlConst.VIDEO_URL;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            FileBody filebodyVideo = new FileBody(new File(params[0]));
            StringBody title = new StringBody("uploadedfile: " + params[0]);
            StringBody description = new StringBody(
                    "This is a video of the agent");
            // StringBody code = new StringBody(realtorCodeStr);

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("uploadedfile", filebodyVideo);
            reqEntity.addPart("title", title);
            reqEntity.addPart("description", description);
            // reqEntity.adddPart("code", code);
            httppost.setEntity(reqEntity);

            // DEBUG
            System.out.println("executing request "
                    + httppost.getRequestLine());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            // DEBUG
            StatusLine status = response.getStatusLine();
            int statusCode = status.getStatusCode();
            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println(EntityUtils.toString(resEntity));
            } // end if

            if (resEntity != null) {
                resEntity.consumeContent();
            } // end if
            if (statusCode == 200) {
                InputStream content = resEntity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                jsonObject = new JSONObject(builder.toString());
                return jsonObject;
            } else {
                Log.e(LoginActivity.class.toString(),
                        "Failed to download file");
            }
            httpclient.getConnectionManager().shutdown();

        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        progressDialog.dismiss();
        if (result != null) {

            try {

                JSONObject jsonObject = result
                        .getJSONObject(ParsingTagConst.COMMANDRESULT);
                String strSuccess = jsonObject
                        .getString(ParsingTagConst.SUCCESS);
                String responseString = jsonObject
                        .getString(ParsingTagConst.RESPONSE_STRING);
                Toast.makeText(RecordingActivity.this, "" + responseString,
                        Toast.LENGTH_LONG).show();
                if (strSuccess.equals("1")) {
                    // get here your response
                }

            } catch (Exception e) {
                // TODO: handle exception
            }
        }

    }

}



enter code here

I have done using following method. you can try this.

//paramString="text you want to put in caption"

private void postPhotoTumblr(String uploadedImagePhotoUrl, String paramString)
{
  CommonsHttpOAuthConsumer localCommonsHttpOAuthConsumer = getTumblrConsumer();
  String str1 = "logged in username";
  String encodedImage = uploadedImagePhotoUrl;
  DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
  HttpPost localHttpPost = new HttpPost("http://api.tumblr.com/v2/blog/" + str1 + ".tumblr.com/post");
  try
  {

    ArrayList localArrayList = new ArrayList();
    localArrayList.add(new BasicNameValuePair("type", "photo"));
    BasicNameValuePair localBasicNameValuePair = new BasicNameValuePair("caption", paramString);
    localArrayList.add(localBasicNameValuePair);
    localArrayList.add(new BasicNameValuePair("data",encodedImage));
    UrlEncodedFormEntity localUrlEncodedFormEntity = new UrlEncodedFormEntity(localArrayList);
    localHttpPost.setEntity(localUrlEncodedFormEntity);
    localCommonsHttpOAuthConsumer.sign(localHttpPost);
    InputStream localInputStream = localDefaultHttpClient.execute(localHttpPost).getEntity().getContent();
    InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream);
    BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
    StringBuilder localStringBuilder = new StringBuilder();
    while (true)
    {
      String str2 = localBufferedReader.readLine();
      if (str2 == null)
      {
        Log.i("DATA post resp", localStringBuilder.toString());
        break;
      }
      localStringBuilder.append(str2);
    }
  }
  catch (ClientProtocolException localClientProtocolException)
  {
    localClientProtocolException.printStackTrace();
  }
  catch (IOException localIOException)
  {
    localIOException.printStackTrace();
  }
  catch (OAuthMessageSignerException localOAuthMessageSignerException)
  {
    localOAuthMessageSignerException.printStackTrace();
  }
  catch (OAuthExpectationFailedException localOAuthExpectationFailedException)
  {
    localOAuthExpectationFailedException.printStackTrace();
  }
  catch (OAuthCommunicationException localOAuthCommunicationException)
  {
    localOAuthCommunicationException.printStackTrace();
  }
}

EDIT : First Upload image to Web Server then get Url and try to Post with uploaded Url or File path. it will work fine sure... :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!