發表文章

設定ImageView圖片的幾種方式

直接設定Resource ID imageView.setImageResource(R.drawable.your_image); Uri直接設定 import android.net.Uri; Uri uri = 圖片/文件選取器取的Uri imageView.setImageURI(uri); 從Uri開啟檔案再設定 import android.net.Uri; import android.graphics.BitmapFactory; try {     InputStream inputStream = getContentResolver().openInputStream(uri);     imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream)); } catch (FileNotFoundException fnf) { } uri如果是Content://這種的,基本上都需要取得READ_EXTERNAL_STORAGE權限。 從實際路徑載入檔案 import android.graphics.Bitmap; String path = 看你從哪裡拿來的路徑 Bitmap bmp = BitmapFactory.decodeFile(path); if (bmp != null) {     imageView.setImageBitmap(bmp); } 如果不是APP自己底下的檔案路徑,都需要取得READ_EXTERNAL_STORAGE權限。

長按ImageView開啟選取器取得圖片Uri

加入長按事件     private ImageView imageView; ... onCreate ...         imageView = findViewById(R.id.imageView);         imageView.setOnLongClickListener(new View.OnLongClickListener() {             @Override             public boolean onLongClick(View view) {                 pickFromGallery();                 return true;             }         }); 啟動圖片選取器     /**      * @ref https://androidclarified.com/pick-image-gallery-camera-android/      */     final static int GALLERY_REQUEST_CODE = 0x1000;     private void pickFromGallery() {         Intent intent = new Intent(Intent.ACTION_PICK); // launch gallery app         //Intent intent ...

筆記:github page

如果你要創建一個xyz.github.io的github page github註冊使用者xyz  創建一個repository名為xyz.github.io (選項)將branch取名為gh-pages (可以從Setting分頁選擇,已不是必要步驟) 將網頁圖文資料加入到該branch,並將分支上傳到github。 進入該repository的Settings分頁,選擇GitHub Pages要使用的branch (預設上傳完應該自動就選好了)。 瀏覽你的github page https://xyz.github.io 如果你有其他repository也是可以設定github page,只是預設會是xyz.github.io/respository_name 如果你有自己的CNAME,也可以在GitHub Pages上設定指到github page。 缺點: 只能放靜態網頁。 所有資料都是公開的。 有網站容量限制。 有流量及瀏覽量限制。 Github官方Steps https://help.github.com/en/github/working-with-github-pages/getting-started-with-github-pages 或參考龍哥的介紹 https://gitbook.tw/chapters/github/using-github-pages.html

筆記:分享圖片/文字/HTML到其他App

分享文字/圖片到其他App,例如FB、Line 以前分享文字 以前分享文字,自己寫intent,然後開啟Activity static Intent getSendTextIntent(String text) {     Intent intent = new Intent();     intent.setAction(Intent.ACTION_SEND);     intent.setType("text/plain");     intent.putExtra(Intent.EXTRA_TEXT, text);     return intent; } startActivity(getSendTextIntent("Hello, just do a test!")); 一般還會建議應該再判斷是否可以啟動Activity if (intent.resolveActivity(getPackageManager()) != null) {     startActivity(intent); } 現在分享文字 現在用ShareCompat就好了 import android.support.v4.app. ShareCompat ; 最新的JetPack是引用 import androidx.core.app. ShareCompat ; ShareCompat.IntentBuilder.from( MainActivity .this)         .setType("text/plain")         .setChooserTitle("Share Text")         .setText("Hello, just do a test!")         .startChooser(); 如果要Intent也是可以getIntent...

取得最上層繪製權限

在SDK 23之前,最上層繪製是不需要取得權限的。 在SDK 23之後,最上層繪製需要於啟動系統設定頁處理。 canDrawOverlays用以確認是否可以最上層繪製。 這裡我曾在SONY手機Android 8.0上遇到一個坑。 APP取得權限後馬上使用canDrawOverlays,實際上是已經擁有權限,但是不論如何都無法獲得正確數值,必須關閉APP再重啟才可以確認已擁有權限,這點在網路上也有人分析是系統問題,必須升級系統才會正常。 static public Intent getDrawOverlayPermissionIntent (Context context) { Intent intent = null; if (Build.VERSION. SDK_INT >= 23 ) { intent = new Intent( Settings. ACTION_MANAGE_OVERLAY_PERMISSION , Uri. parse ( "package:" + context.getPackageName())) ; // Workaround: put extras for the key event not handled in Overlay Setting page. intent.putExtra( "extra_prefs_show_button_bar" , true ) ; // show the status bar intent.putExtra( "extra_prefs_show_skip" , false ) ; // skip button is gone intent.putExtra( "extra_prefs_set_next_text" , "" ) ; // next button is gone intent.putExtra( "extra_prefs_set_back_text" , ...

彙整一些取得Android Intent的方便函數

取得Android Home,Launcher2, Launcher3(Androi 7.1/TV), 設定頁, Wifi設定頁 static public Intent getAndroidHomeActivityIntent () { Intent intent = new Intent(Intent. ACTION_MAIN ) ; intent.addCategory(Intent. CATEGORY_HOME ) ; intent.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK ) ; return intent ; } static public Intent getLauncher2ActivityIntent () { Intent intent = new Intent(Intent. ACTION_MAIN ) ; intent.setComponent(ComponentName. unflattenFromString ( "com.android.launcher/com.android.launcher2.Launcher" )) ; intent.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK ) ; return intent ; } static public Intent getLauncher3ActivityIntent () { Intent intent = new Intent(Intent. ACTION_MAIN ) ; intent.setComponent(ComponentName. unflattenFromString ( "com.android.launcher3/com.android.launcher3.Launcher" )) ; intent.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK ) ; return intent ; } static public Intent getSettingActivi...

筆記:在heroku建立免費line bot

圖片
主要參考資料 使用Node.js在Heroku上架設Line機器人 https://medium.com/@wanwanyang/60ec3f6cac7f Line官方文件 https://developers.line.biz/en/docs/messaging-api/building-sample-bot-with-heroku/ 前置知識 git node.js ubuntu 1. 建立Line開發者帳號 從這裡 進入 ,可以用個人用的一般Line帳號登入,登入Developers console時會請我們再填入 名字 跟 E-mail 就照著填。 Line的帳號 分類 ,簡略來說就是 Reply和Push API對開發者不收費但有限制好友數目 免費版則只能用Reply API,也只能文字訊息 付費用Push API至少要到進階版(API) 收費3,888/月 【加值服務教學】升級方案、取消付款與確認購買狀態 http://at-blog.line.me/tw/archives/63198901.html#Q6 Line API差異 Reply API僅能回覆訊息,使用者一問機器人一答的情境,無法回多個訊息。 Push API就是Line bot可以主動發送訊息給加好友的使用者,或是分多次訊息發話。 2. 建立bot用的Channel並找出secret和token 依照Line 官方步驟 建立channel,從developers console中選擇"Create new provider"。 隨後channel類型選擇"Messaging API",並輸出各種channel的資訊即可完成建立。底下兩個方塊記得打勾。 可參考: https://developers.line.biz/en/docs/messaging-api/building-bot/ https://developers.line.biz/en/docs/messaging-api/building-sample-bot-with-heroku/ 2.1 找出Channel Secret 在Chan...

line bot推送多訊息

1. 事前準備: (1) 取得對方的User ID 需請對方加bot好友。對方封鎖bot好友時,對方會收不到發送的訊息。 (2) 填入將User ID填入變數 取代下面程式碼的targetUID/TARGETUID的值。 (3) 設定環境變數 export CHANNEL_ACCESS_TOKEN=你的line bot的access token 2. 使用node.js發送 const targetUID = 'U1fcb...'; const line = require('@line/bot-sdk'); const client = new line.Client({   channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN, }); const msg1 = { type: 'text', text: 'Hello World1' }; const msg2 = { type: 'text', text: 'Hello World2' }; client.pushMessage(targetUID, msg1)   .then(() => {     console.log('send1 ok!');     client.pushMessage(targetUID, msg2)     .then(() => {       console.log('send2 ok!');     })     .catch((err) => {       console.error(err);     });   })   .catch((err) => {     console.error(err);   }); 2.1 執行範例 若上面程式碼命名為send.js則 node send.js 3. 使用CURL發送@Linux export TARGE...

用CloudWatch定時產生Event丟檔案到S3

圖片
1. 建立S3 Bucket 建立後記得改後面的程式碼的var bucketName變數的值。 2. IAM新增Role  先在IAM新增一個role,連接AWSLambdaExecute政策,這個政策會包含可以在S3放檔案的權限。 3. 新增Lambda函數 選擇"從頭開始撰寫" 函數名稱,看你喜歡,我是設"myLambda" 執行時間,使用"Node.js 12.x" 選擇或建立執行角色,使用已存在角色,選擇步驟1所建立的Role。 放置Lambda程式碼 var AWS = require('aws-sdk'); var bucketName = '{your-bucket-name}'; function putFileToS3(key, body, callback) {     var s3 = new AWS.S3();     s3.putObject({Bucket: bucketName, Body: body, Key: key}, callback); } exports.handler = (event, context, callback) => {     console.log('my lambda');     console.log('Received event:', JSON.stringify(event, null, 2));     putFileToS3(event.id, JSON.stringify(event, null, 2),         (err, response) => {             if (err) {                 console.error(err);                 callback(err); ...

筆記:放置AWS安全登入資訊及存取金鑰

圖片
在閱讀 AWS 文件 後,得知需建立 存取金鑰(Access key) ,要注意事後若忘記 金鑰, AWS 也不會再提供 ,只能從主控台中產生新的 金鑰 ,並廢止前一組 金鑰。 我們可以在安全登入資料( security credentials page) 中建立根密鑰( root secret key) ,這個設定可從 Identity and Access Management (IAM) 進入。 由於直接從安全登入資料所建立的存取金鑰 是沒有限制存取資源的,所以 官方 建議要先建立一個 IAM user ,再創建這個使用者使用的存取金鑰,以便 限制存取的資源。 我可以在 AWS IAM user 中新增一個 使用者,並給予 必要 權限以方便後續測試。創建使用者後,點進去使用者細節,就可以在分頁"安全登入資料" 中 創建存取金鑰 。 建立時會像下面這樣,記得要下載,不然之後AWS也不會再提供了。你只能建立新的 AWS 安全登入資訊應放置得位置 Linux使用者為 ~/.aws/credentials  Windows 使用者則為  C:\Users\{YOUR_USER_NAME}\.aws\credentials [default] aws_access_key_id = YOUR_ACCESS_KEY_ID aws_secret_access_key = YOUR_SECRET_ACCESS_KEY