2016年1月13日 星期三

[Android] VLC 3.0 Library 分享

因工作需求需要使用到VLC的一些功能
官網下載原始碼自行編譯

Source code: http://code.videolan.org/videolan/vlc-android
Compile tutorial: http://wiki.videolan.org/AndroidCompile/

我的os平台是Ubuntu 15.10, 在編譯過程當中我碰到的一些問題:

(1) 修改gettext版本
Note : VLC requires gettext version 0.19.3, but Ubuntu version 15.04 (Vivid Vervet) provides version 0.19.2
To build VLC with this release, either build your own gettext from sources, or edit vlc/contrib/contrib-tizen-arm-linux-gnueabi/libgpg-error/configure.ac, line 158 and change version 0.19.3 to version 0.19.2
 
(2) 如果出現_Static_assert(x, s) blablabla..之類的錯誤請打開vlc_fixups.h 
位置在(/source code folder../vlc/include)
將#define _Static_assert(x, s) ((void)sizeof (struct {unsigned: -!(x);})) 註解掉


若不想自行編譯, 我將我自己編好的.aar函式庫分享到google drive上, 請自行下載使用
版本有:armeabi armeabi-v7a x86 mips
https://drive.google.com/file/d/0B5z0azUwvd7yQ0tHMjBkcFlETHc/view?usp=sharing

Android Studio使用方法:
File->New->New Module->Import .JAR/.AAR Package 選擇你要使用的library
import進來後, 再設定dependency就好了
File->Project Structure->左邊列表的Modules->app->上方的Dependencies分頁-> 按右上角的+號 -> Module dependency-> 選擇:libvlc-3.0.0



若有編譯上的問題, 歡迎提出來一起討論~

2015年11月17日 星期二

[Android] OpenCV - "java.lang.UnsatisfiedLinkError: dlopen failed: library "libopencv_java3.so" not found"

Please copy the "libopencv_java3.so"  from OpenCV sdk folder to app's jniLibs folder

The file location here:
...\OpenCV\sdk\native\libs\**Each platforms**\libopencv_java3.so

2015年10月11日 星期日

[Android] Memo - RecyclerView使用事項

RecyclerView是被放在android.support.v7.widget裡,使用前需要開啟該app的gradle檔加入

compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'

2015年7月29日 星期三

[Android] 其他class使用MainActivity元件

MainActivity.java

static MainActivity ths;

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    ....
    ths = this;
    TextView showName = (TextView)findViewById(R.id.showName);

    public static MainActivity getInstance()
    {
       return ths; 
    }

    ....

    public void updateTheTextView(final String t) {
      MainActivity.this.runOnUiThread(new Runnable() {
         public void run() {
             showName.append(t);
         }
      });
    }
}




Others.java
使用MainActivity的texview就只要如下:

MainActivity.getInstance().updateTheTextView(strAddress);

[Android] Handler should be set to static to avoid memory leak

腦弱, 每次都忘記怎麼寫, 還要去開以前的code出來, 紀錄在這比較快 XD


private final MyHandler myhandler = new MyHandler(this);

.....

public static class MyHandler extends Handler {
    private final WeakReference<MainActivity> mActivity;

    public MyHandler(MainActivity activity) {
        mActivity = new WeakReference<>(activity);
    }

    @Override    public void handleMessage(Message msg) {
        final MainActivity activity = mActivity.get();
        if (activity != null) {
            // TODO: implement your code here

        }
    }
}

2015年4月29日 星期三

[Android] Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

今天看書練習Broadcast receiver時, 在自定義的BroadcastReceiver的類別onReceive()中, 我使用了一個intent想要將廣播收到的值抓出來再丟去瀏覽器查詢, 如下

    @Override
    public void onReceive(Context context, Intent intent) {
        String strQuery= intent.getStringExtra("QUERY_PARM");
        Uri uri = Uri.parse("https://www.google.com.tw/webhp?hl=zh-TW&gws_rd=ssl#hl=zh-TW&q=" + strQuery);

        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(it);
    }


但收到廣播後要開啟瀏覽器前, 程式就掛掉惹, 且DDMS出現了以下訊息:
 Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?


請問Google大神後, 發現如果是用context執行starActivity, 就需要再開一個新的task
解決方法就是在startActivity之前指派一個flag給intent 就OK了

        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(it);

2015年4月10日 星期五

[Android] How to use facebook SDK 4.0.0 to share image.

看圖說故事囉~

新增專案

引入module


Facebook SDK下載位址: https://developers.facebook.com/docs/android 下載後請解壓縮
找到下載的Facebook sdk位址按下確定

Sync一下發現出現問題 ANDROID_BUILD_SDK_VERSION

打開facebook的 build.gradle檔, 將四個變數, 複製到gradle.properties

填入版本, 與app的build.gradle相同就好

再Sync一下, 又有問題了 = ="

這次請打開專案的build.gradle 將classpath的gradle版本從1.1.0修改為1.1.3

打開Project Structure (Ctrl+Alt+Shft+S)
照下圖修改一下

點選app->Dependencies->+號->選擇:facebook->OK

打開Facebook developer網頁 https://developers.facebook.com/apps/
新增一個App

選擇Android

填入你想要命名的app名稱

按下Create App ID

 填入你程式的package name和預設進入的Activity名字(通常沒改的話都是MainActivity)

下一步會要求你填入Key Hash

這裡我們要借助剛剛的程式把以下code填到onCreate()裡面,來取得Key Hash (當然你也可以照Facebook guide的建議方式取得): 請注意要修改的地方
 
PackageInfo info;
try {
  info = getPackageManager().getPackageInfo("com.foxlinkimage.alex.facebooksharedemo", PackageManager.GET_SIGNATURES);
  for(Signature signature : info.signatures)
  {      
      MessageDigest md;
      md =MessageDigest.getInstance("SHA");
      md.update(signature.toByteArray());
      String KeyResult =new String(Base64.encode(md.digest(), 0));//String something = new String(Base64.encodeBytes(md.digest()));
      Log.e("hash key", KeyResult);
      Toast.makeText(MainActivity.this, "My FB Key is \n" + KeyResult, Toast.LENGTH_LONG).show();
   }
}catch(PackageManager.NameNotFoundException e1){Log.e("name not found", e1.toString());
}catch(NoSuchAlgorithmException e){Log.e("no such an algorithm", e.toString());
}catch(Exception e){Log.e("exception", e.toString());}



DDMS中填入hash key 並Log level改成Error, 接上手機(開啟模擬器), 按下Debug, 會在下面框框跳出你的hash key

回到網頁上填入它吧! 按下一步, 再回到程式開始進行工作

- 打開Manifest.xml檔
  1. 新增權限, 這裡會用到網路和讀取SD卡
  2. 在<application>標籤裡填入meta-data
 
 < meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id">
在strings.xml新增一個變數app_id, 這個ID就是你剛剛申請時facebook發配給你的, 可以到剛剛那個網頁找到.
往下拉, 新增FacebookActivity和provider, 請注意在provider內後面請改成你的app_id
 
<activity android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" android:name="com.facebook.FacebookActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar">

<provider android:authorities="com.facebook.app.FacebookContentProvider456793911146084" android:exported="true" android:name="com.facebook.FacebookContentProvider">
加入一個按鈕
照key啦~在此僅列出onClick()內的程式碼: 
請注意在onCreate()最一開始要先初始化FacebookSdk元件
 
FacebookSdk.sdkInitialize(getApplicationContext());
 
Bitmap image = BitmapFactory.decodeFile(path);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
shareDialog.show(content);

噫~好了~ 可以用哩