본문 바로가기

개발관련 정보/안드로이드

Pinterest 공유걸기~

우선 아래 두 파일을 pinterest 패키지에 담고~



# PinterestHelper.java


package com.hongeuichan.pinterest;


import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.util.Log;

import android.widget.ImageView;


import java.net.URL;


public class PinterestHelper {


    private static final String TAG = "PinterestHelper";


    public static class RemoteImageTask extends AsyncTask<Void, Void, Bitmap> {

        ImageView _image;

        String _imageSource;

        TaskCallback _callback;


        public RemoteImageTask(ImageView image, String imageSource) {

            this(image, imageSource, null);

        }


        public RemoteImageTask(ImageView image, String imageSource, TaskCallback callback) {

            _image = image;

            _imageSource = imageSource;

            _callback = callback;

        }


        protected Bitmap doInBackground(Void... params) {

            URL url;

            Bitmap bmp = null;

            try {

                url = new URL(_imageSource);

                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

            } catch (Exception ignored) {

                Log.e(TAG, "Exception", ignored);

            }


            return bmp;

        }


        protected void onPostExecute(Bitmap bmp) {

            _image.setImageBitmap(bmp);

            if (_callback != null)

                _callback.onTaskFinished(bmp);

        }

    }


    public interface TaskCallback {

        public void onTaskFinished(final Bitmap bmp);

    }

}


# SendPinterest.java


package com.hongeuichan.pinterest;


import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.TextView;


import com.hongeuichan.morebar.R;

import com.pinterest.pinit.PinItButton;

import com.pinterest.pinit.PinItListener;


/**

 * Pinterest에 공유하기! (뷰가 있지만,. 내용을 셋팅하고 바로 클릭을 실행하여 sdk에 설정된 형태로 보여짐!)

 * 

 * @author hongeuichan

 *

 */

public class SendPinterest extends Activity {

static final String TAG = "SendPinterest";

public static final String CLIENT_ID = "1437978";

static Context mContext;

String urlText = null;

String title = null;

String bodyText = null;

String imgUrl = null;

private Handler mHandler;

private ImageView mImage;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);


// Activity 흐리게 노출


WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();

layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;

layoutParams.dimAmount = 0.7f;

getWindow().setAttributes(layoutParams);

setContentView(R.layout.share_pinterest);

mContext = getBaseContext();

urlText = getIntent().getExtras().getString("urltext");

        title = getIntent().getExtras().getString("title");

        bodyText = getIntent().getExtras().getString("bodytext");

        imgUrl = getIntent().getExtras().getString("imgurl");

        

        mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 0: {

finish();

break;

}

default:

break;

}

}

        };

        

        PinItButton.setDebugMode(true);

        PinItButton.setPartnerId(CLIENT_ID);

        

        ((TextView) findViewById(R.id.desc_tv)).setText(title);

        ((TextView) findViewById(R.id.pin_text)).setText(bodyText);

        mImage = (ImageView) findViewById(R.id.source_iv);

        

     // 닫기 버튼 선택 시

  ((ImageButton) findViewById(R.id.pin_closebtn)).setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  mHandler.sendEmptyMessage(0);

  }

  });

 

        // Show the remote image in ImageView.

        new PinterestHelper.RemoteImageTask(mImage, imgUrl).execute();

   

        PinItButton pinIt = (PinItButton) findViewById(R.id.pin_bt);

        pinIt.setImageUrl(imgUrl);

        pinIt.setUrl(urlText);

        pinIt.setDescription(bodyText);

        pinIt.setListener(_listener);

        

        // 에잇... 자동으로 클릭하자!

        pinIt.performClick();

        

}

PinItListener _listener = new PinItListener() {


        @Override

        public void onStart() {

            super.onStart();

            Log.i(TAG, "PinItListener.onStart");

        }


        @Override

        public void onComplete(boolean completed) {

            super.onComplete(completed);

            Log.i(TAG, "PinItListener.onComplete");

            mHandler.sendEmptyMessage(0);

        }


        @Override

        public void onException(Exception e) {

            super.onException(e);

            Log.e(TAG, "PinItListener.onException");

        }


    };

}



그리고~

intent로 호출하여 공유한다~

intent = new Intent(mContext, SendPinterest.class);

intent.putExtra("urltext", "");

intent.putExtra("title", title);

intent.putExtra("bodytext", bodytext);

intent.putExtra("imgurl", SrcURL+fname);

intent.putExtra("photo", imgByte);

startActivity(intent);


타이틀, 텍스트, 이미지경로~ 그리고 imgByte.. 이것은 Bitmap 이미지 를 byte[]형으로 지정함.

byte[] imgByte;


외부에서 넘겨받아 처리할 경우

imgByte = getIntent().getExtras().getByteArray("photo");


imgurl은 로컬디바이스에 저장된 이미지 경로를 지정한다.