Facebook SDK集成之分享篇

前情概要

  1. Facebook SDK集成之綱要篇
  2. Facebook SDK集成之準備篇
  3. Facebook SDK集成之登錄篇

集成環境

github上的環境說明有點瑕疵, 在Wosao/readme上做了更正。本地集成環境如下:
1. AndroidStudio 1.4.1
2. Facebook SDK 4.6.0
3. Gradle 2.2
4. OS Win10

目標

本作是其中的第四篇, 通過本作,您可以瞭解如下內容:
1. 分享默認圖片
2. 選擇圖片分享。

分享默認圖片

本作代碼 使用 git checkout -f MyDev-06-ShareDefaultImage 獲得

  • 首先,在act_home中添加xml 元素

        <!-- 分享圖片-->
        <LinearLayout
            android:id="@+id/ll_share_container"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="horizontal"
            android:layout_above="@+id/ll_social_container"
            android:layout_marginBottom="30dp">
    
            <LinearLayout
                android:id="@+id/ll_share1_container"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1">
    
                <ImageView
                    android:id="@+id/im_icon_share_default"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:layout_weight="5"
                    android:src="@drawable/icon_share"
                    />
                <Button
                    android:id="@+id/bt_share1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button_share_txt"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    />
    
            </LinearLayout>
            <LinearLayout
                android:id="@+id/ll_share2_container"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1">
    
                <ImageView
                    android:id="@+id/im_icon_share_select"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:layout_weight="5"
                    android:text="@string/iv_select_file"
                    />
                <Button
                    android:id="@+id/bt_share2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button_share_txt"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    />
    
            </LinearLayout>
    
        </LinearLayout>

    注意,這裏添加了多個佈局文件,爲了後續選擇圖片分享的實現。而且爲了等分左右屏幕,頻繁使用android:weight, 在實際開發過程中,儘量避免頻繁使用!

  • 其次, 在代碼中添加

        //分享圖片
        private Button shareDefaultImage;
            //1)分享默認圖片
            shareDefaultImage = (Button)findViewById(R.id.bt_share1);
            shareDefaultImage.setOnClickListener(this);
                case R.id.bt_share1:
                    //爲了分別說明分享步驟,這裏再次重複寫登錄步驟
                    Log.e(TAG, "onClick...share default image");
                    login4ShareImage();
                    break;
  • 最後, 在login4ShareImage中重複寫了登錄facebook的步驟
    關鍵代碼段如下:

        private void publishImage() {
            //Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.icon_share );
            SharePhoto photo = new SharePhoto.Builder()
                    .setBitmap(image)
                    .setCaption("Just for testing!!")
                    .build();
            SharePhotoContent content = new SharePhotoContent.Builder()
                    .addPhoto(photo)
                    .build();
    
            ShareApi.share(content, null);
        }

選擇圖片分享

本作代碼 使用 git checkout -f MyDev-07-ShareSeletedImage 獲得

這個主題需要添加的內容比較多:

  • 選擇圖片
  • 生成特定寬高bitmap

選擇圖片

  • 打開文件管理器

        private void openFileManager() {
            Intent it = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            it.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    
            try {
                startActivityForResult(it, REQUEST_CODE);
            } catch (ActivityNotFoundException exp) {
                Toast.makeText(this, getResources().getString(R.string.view_no_filemanager),
                        Toast.LENGTH_SHORT);
            } catch (Exception exp) {
                Toast.makeText(this, getResources().getString(R.string.view_filemanager_error),
                        Toast.LENGTH_SHORT);
            }
        }
  • 回調處理
    需要在onActivityResult中處理

    else if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {//獲得選中的圖片Uri
                Log.e(TAG, "onActivityResult(): data = " + data);
                getImageInfo(data);
            }
  • 獲取必要信息

        private void getImageInfo(Intent data) {
            if (data == null) {
                Log.e(TAG, "data is null!");
                return;
            }
            Uri selectedUri = data.getData();
            Cursor cursor = null;
            String mimeType;
    
            try {
                cursor = getContentResolver().query(selectedUri, null, null, null, null);
                Log.e(TAG, "\n selectedUri = " + selectedUri + "\n cursor = " + cursor
                        + "\n started string = "
                        + selectedUri.toString().substring(0, 7));
    
                if (selectedUri != null
                        && selectedUri.toString().substring(0, 10)
                        .equals("content://") && cursor != null
                        && cursor.moveToFirst()) {
    
                    mimeType = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
                    Log.e(TAG, "mimeType  = " + mimeType);
    
                    imagePath = cursor.getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    Log.e(TAG, "imagePath = " + imagePath);
                }
    
                // ImageView顯示圖片
                //1. 爲了UI效果的美觀, 2等分佈局, 需要獲得imageview 大小, 然後將選擇的圖片縮放爲imageview大小
                //**注意:在onCreate中使用getHeight & getWidth獲得控件的寬高是不行的,這是因爲他們自己還沒有被度量好**
                Bitmap bitmap = null;
                mHeight = selectedImage.getHeight();
                mWidth = selectedImage.getWidth();
                Log.e(TAG, "width = " + mWidth + ", mHeight = " + mHeight);
    
                //2. 將圖片壓縮成ImageView大小的bitmap
                if (imagePath != null && !imagePath.isEmpty()) {
                    bitmap = Utils.decodeFile(imagePath, mWidth, mHeight);
                    selectedImage.setImageBitmap(bitmap);
                    bitmap.recycle();
                }
    
            }catch (Exception exp) {
                exp.printStackTrace();
                return;
            } finally {
                if (cursor != null && !cursor.isClosed()) {
                    cursor.close();
                }
            }
    
        }

生成特定寬高bitmap

這裏用到了Utils中的2個函數,通過文件路徑獲得bitmap。
- 1. decodeFile(String path)
- 2. decodeFile(String path, int reqWidth, int reqHeight)
詳細的 參看源碼!
在Wosao中 圖片展示使用 [2] 個, 發送給facebook使用 [1] 函數。

sample運行效果

Wosao運行效果

其中黃框分享默認圖片到facebook,籃框分享選擇圖片, 注意籃框上部點擊可以選擇圖片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章