iT邦幫忙

第 11 屆 iThome 鐵人賽

0
自我挑戰組

練習程式系列 第 34

Android 畫面、delete image、Apache(php) , 安裝apache,import library

整理

1

Day 30. 常用快捷鍵 | 應用程式 Icon

android studio重新排版【Ctrl + Alt + L】

> 更換 App Icon

到https://romannurik.github.io/AndroidAssetStudio/ ,製作圖案。
下載檔案後,res資料夾的內容,覆蓋到project模式(在android模式 看不到)。
路徑:app-- >src -- > main -- > res
https://ithelp.ithome.com.tw/upload/images/20191026/20111994O7vdi3PvQp.png

或者可以直接在android studio更改:
How to Change the App Icon in Android Studio (With Adaptive Icons)

2

(轉貼)Android 禁止螢幕旋轉 & 螢幕旋轉不刷新 Activity & 動態更改螢幕方向

3

點擊TextView:
How to click or tap on a TextView text
參考:解答1

4

Android長方型製作:
Create box, square or rectangle shape in XML – Android

5

修改android右上角的選項(Options Menu):
Options Menu with Sub Items - Android Studio Tutorial

Android內建圖案:
https://ithelp.ithome.com.tw/upload/images/20191026/20111994NcOm6LUuFD.png

製作出來的圖示,檔案長這樣:
Fillcolor可以修改圖案顏色:
https://ithelp.ithome.com.tw/upload/images/20191026/20111994o7fk9JnZVu.png

更多標題列修改內容:
Android options menu tutorial

修改標題列的方式:
一 在畫面的xml放toolbar元件,toolbar就是標題列,可以自行修改樣式

二 在manifest的activity加上

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

因為要直接讓標題列消失,再換自訂的toolbar

三 在antivity的oncreate寫:

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        setTitle("我是標題");

6

notifyDataSetChanged():
Android 解决 adapter. notifyDataSetChanged() 不起作用

當adapter的項目改變(arraylist改變之類的),可以用adapter.notifyDataSetChanged();
畫面會變更成改變後的樣子,會執行adapter的getView()方法,讓畫面在刷新一次。

7

How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

邊走訪arraylist時,邊用arraylist的remove,移除元素,會有這個錯誤。
解決方法解答3的留言:
直接用一般的for loop:

for (int i = ArrayList.size()-1; i >-1; i--) {
if (true) {
ArrayList.remove(i);
}
}

8

如何刪除相簿的相片:
android : deleting an image

解答5和留言區(兩種刪除方法都用):

不管是contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            MediaStore.Images.ImageColumns.DATA + "=?" , new String[]{ imagePath });
還是String file_dj_path = Environment.getExternalStorageDirectory() + "/ECP_Screenshots/abc.jpg";
        File fdelete = new File(file_dj_path);
都使用就成功了。

程式:

File fdelete = new File(PATH);
 ContentResolver contentResolver = getContentResolver();

 if (fdelete.exists()) {
  if (fdelete.delete()) {
   contentResolver.dele(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    MediaStore.Images.ImageColumns.DATA + "=?", new String[] {
     PATH
    });
   adapter.notifyDataSetChanged();
  } else {
   Log.e(TAG, "file not Deleted");
  }
 }

9 android和mysql連線

完整程式步驟:
Android Login/Registration with MySQL Database Example Using Retrofit

完整軟體安裝步驟:
How to Install Full Web-Server (Apache, php, MySQL, phpMyAdmin) In Windows 10

遇到問題:

php指令可能要重新啟動電腦才能使用,但是apache伺服器使用php好像不需用重啟電腦(還是要重啟apache伺服器?):
設定環境變數讓 Windows cmd 命令列可以執行 php 指令


如果原本就有別的網頁伺服器在電腦跑,然後又多了apache伺服器的網頁,那要怎麼辦?
修改port:Can I run two web servers on the same computer?
Configure Apache Web Site to Use Multiple Ports


php7.0要跟Apache2.4綁定的樣子,版本要注意,不然可能會有錯誤,像是:
Apache doesn't load module from PHP
PHP 安装遇到的坑Cannot load php7apache2_4.dll into server
因為載到32位元的php才有這個錯誤,要載64位元。


【Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...
Day 23 - OkHttp網路連線
Day 24 - 使用Retrofit與API連線


回傳 JSON 記得送 header
php記得加

header('Content-Type: application/json; charset=utf-8');

不然Retrofit收不到:
解答2:
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $


PHP 如何做到~統計下載的次數

原本想參考這個解答的方式,下載檔案(.apk)。因為這樣好像就不會直接看到檔案路徑,和可以先寫一些程式,像是寫紀錄(ip、時間)到資料庫之類的。

所以練習了,1樓大大的解答:
header
http://root.mrs3.tw/test66.php

觀察response header
https://ithelp.ithome.com.tw/upload/images/20200506/201119948KD04Ell4n.png

但是下載的檔案,好像有錯誤,大小都是0KB。可能是apk是MB,比較大,
所以改參考:Big files download through php function readfile not working

其他:
Ccontent-ype 和content- disposition測試
https://ithelp.ithome.com.tw/upload/images/20200511/20111994rHxcE8JJ9D.png


php.ini檔設定:
要注意 upload_max_filesize和post_max_size 要大於檔案大小
不然會有錯誤:

POST Content-Length of 500000 bytes exceeds the limit of 10240 bytes in

像是

post_max_size=10K 

就是post只能10240 bytes

upload_tmp_dir 就是檔案post到apache後,php會先放到一個暫存的目錄,通常都是:

C:\Windows\Temp

但是為什麼沒看到檔案?因為:
https://stackoverflow.com/questions/6946054/where-is-my-uploaded-file

The tmp_name is only valid for the lifetime of the PHP script execution, after which the file is cleaned up.
所以可以在php之後用sleep,就會看到檔案了

sleep(70)  // 檔案70秒左右可以看到,之後就會自動消失了

upload_tmp_dir 也可以自己更改路徑,但是要注意open_basedir
open_basedir就是php可以到達的目錄,如果設定只有D:\ted5
那就只能在D:\ted5下,php才有用。
會有這個Warning:

Warning: Unknown: open_basedir restriction in effect. File(C:\Apache24\htdocs\ted5) is not within the allowed path(s):

預設沒設定,就是php在哪個目錄都可以作用
參考:
Setting PHP tmp dir - PHP upload not working
php.ini - setting upload_tmp_dir
open_basedir 設定多個目錄

其他參考:
如何在不同瀏覽器下載正確的檔案名稱(Content-Disposition)
如何正確的取得使用者 IP?
PHP 學習筆記 header:下載與轉址等等
解除 PHP 上傳大檔案限制,修改 php.ini 設定檔
PHP 上傳檔案程式設計教學,$_FILES 多檔案用法

九:
apache設定位置:/conf/httpd.conf
可能問題:解決apache啟動錯誤 AH00558: httpd: Could not reliably determine...
windows下apache的簡單命令
Update user password in Mysql 5.7

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';

其他php練習:
PHP-Send HTTP Get/Post Request and Read JSON response
php - 在PHP,怎樣處理警告file_get_contents( ) 函數?
PHP : json_decode
Making PHP var_dump() values display one line per value
How to copy a file from one directory to another using PHP?

9.01 android傳圖片到apache伺服器 ,mySQL放檔案路徑。

教學網址:Upload Image in Android Studio using PHP, MySql
android upload image to server -insert path and data using php and mysql

整理
一:
Can I store images in MySQL [duplicate]
參考解答3的方法:圖片檔案存到apache伺服器(檔案目錄),然後mySQL放檔案路徑。

二:
Android - How can I upload multiple files in one request
如果一次上傳多個檔案????
解答1:
取得所有檔案的絕對路徑,用成一個arraylist,在迴圈就可,程式參考解答1(所以還是一個一個傳)

用內建的相簿顯示,感覺功能很少:
Android 如何選取圖片或是檔案?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

所以找到一個第三方類別庫:
zhihu/Matisse
知乎开源Matisse图片选择器使用

9.02 使用Volley傳圖片到mysql

Android Image Upload to App Server using Volley
https://bng86.gitbooks.io/android-third-party-/content/volley.html
Set up RequestQueue
一次多個圖片,直接迴圈,所以實際還是一個一個傳。
(mutipart 多檔案方式,一次http連線就可以傳多個檔案)

來了解非同步(Asynchronous)與同步(Synchronous):
非同步(Asynchronous)與同步(Synchronous)的差異

所以在網路傳東西,都會有請求、回應之類的。在請求之後,會繼續執行接下來的程式,不會在那邊等回應。
如果用迴圈跑很多次網頁請求,迴圈雖然很快跑完了,但是每個請求的回應都還在執行,而且不會照著迴圈順序回應。
所以如果要一個請求跑完,在一個請求繼續跑,要怎麼辦?
就是在回應那邊寫遞迴,呼叫自己:
how to make volley request in loop until specific response come
How to perform loop with volley

再來了解Volley超時重置:Volley超时重试机制

傳圖片壓縮

圖片壓縮後,傳的時間會縮短幾秒,參考:
深入理解 Android 中的 Matrix
Compress bitmap to a specific byte size in Android
Android Bitmap最全面详解
取得圖片matrix的值:

var height = imageview.drawable.bounds.height()
var width = imageview.drawable.bounds.width()
var matrix_value : FloatArray =   FloatArray(10);
imageview.getValues(matrix_value)
for(matrix_number in matrix_value){
    Log.w(TAG,"matrix_number:"+matrix_number)
 }

Volley Singleton Pattern

Singleton教學:Singleton Model: Android Programming
Set up RequestQueue
Volley Singleton Pattern - Android Studio Tutorial

如果要自己改一些第三方類別庫的東西,要怎麼改???

直接載下來,放在自己的專案:(不確定以下方法正確,每個類別庫要改的東西好像不同)

import library步驟:
import module
把github載下來的壓縮檔,import module


build.gradle(Module:app)寫:

implementation project(':matisse')

就不用寫:

def matisse_version = "0.5.0-alpha4"
implementation "com.zhihu.android:matisse:$matisse_version"

這個也沒寫(Module:Project):

classpath 'com.novoda:bintray-release:0.8.0'

就可以用了。

10

在gridview的圖示,長按圖示後,顯示X,代表要刪除
參考: Android进阶——GridView实现可长按item显示可删除的小图标的UI

但是照著做,會有一個問題,長案後,全部X都會跑出來。所以在arraylist再加一個boolean項目控制圖案要不要顯示:( true代表要刪除、要顯示圖案;false代表不要刪除、不顯示圖案)

11 android圖片點擊動畫

android imageview onClick animation
參考解答1

12 修改標題列的右上角文字

manifests檔增加theme:

        <activity
            android:name=".PictureActivity"
            android:theme="@style/fullViewTheme" />

value/style檔,假設標題列改成fullViewTheme:

    <style name="fullViewTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:actionMenuTextAppearance">@style/MenuTextStyle</item>
    </style>
    <style name="MenuTextStyle">
        <item name="android:textSize">30sp</item>
    </style>

13 想要改圖片檔名(失敗)

Java實現簡單修改檔名的方法分析

但是:renameTo

需要Write permission和Search permission。
Search permission是sinagture permission,不能在一般應用程式使用???

14

InflateException: Binary XML file line #8: Error inflating class ImageView
版本24 大於 手機的SDK版本 ,所以圖片錯誤:
解決:
project把drawable-v24的檔案移動到drawable:
https://ithelp.ithome.com.tw/upload/images/20191116/20111994bsf0KG5y9n.png

15 標題列的下拉選單

Android Spinner Toolbar Tutorial
GiveMePasS's Android惡補筆記
Android Spinner Toolbar Tutorial
Disabling the first item in a spinner

spinner.setSelection(position, false);

change Spinner text color

16 filtered、search

In this video we will learn how to use the SearchView widget to filter a RecyclerView in real time.
How to create a custom filtered adapter in Android
使用arraylist筆記:如果要把arraylist1內容複製給arraylist2,但是之後不會跟著改變,要用addall,不能直接arraylist2 = arraylist1:

        var picList: ArrayList<String> = arrayListOf()
        var picList1: ArrayList<String> = arrayListOf()

        picList = picList1

        picList1.add("a")
        picList1.add("b")
        picList1.add("c")

        for(pic in picList1){
            print(pic)
        }

        for(pic in picList){
            print(pic)
        }

結果:

abcabc

17

Use “ENTER” key on softkeyboard instead of clicking button

18

Android Studio中怎么设置中文应用名


上一篇
Android ,存圖片到SQLite,process、thread、ByteArrayOutputStream
下一篇
WEB 筆記
系列文
練習程式37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言