iT邦幫忙

第 11 屆 iT 邦幫忙鐵人賽

0
自我挑戰組

練習程式系列 第 33

Android ,存圖片到SQLite,process、thread、ByteArrayOutputStream

教學來源:
Saving Image And Text Into SQLite database - Custom GridView in Android (Part 1/3)

整理:

1 java thread

[Java] 執行緒(Thread) 入門
OS - Ch4 多執行緒 Multithread Programming

Program(程式):程式檔案
Process(程序):跑起來的程式
Thread(執行緒):包含在 process 之中,是 process 中的實際運作單位。

程式練習一:
Thread1執行t2.join();。所以會先顯示Thread2,再顯示Thread1

public class threadTest {
    static class Thread1 extends Thread {
        public void run(){
           try {
                Thread t2 = new Thread2();
                t2.start();
                //Thread.yield();
                t2.join();
                sleep(2000);
            } catch(InterruptedException e){

           }
            System.out.println("I'm Thread1");
        }
    }

    static class  Thread2 extends Thread {
        public  void run(){
           try {
                sleep(2000);
            } catch(InterruptedException e){

            }
            System.out.println("I'm Thread2");
        }
    }
    public static void main(String[] args){
        Thread t1 = new Thread1();
        t1.start();
    }
}

程式練習二:
來源:Day4:寫簡單 Java Thread Sample Code (二)
thread有兩種寫法,一種是extends Thread,另一種是implements Runnable。
implements Runnable的方法比較推薦。

程式練習三:
Java ExecutorService - Part 1 - Introduction
Java Thread Pool – ThreadPoolExecutor Example

C process

教學來源:
[Linux C] fork 觀念由淺入深
OS - Ch3 行程 Process

由於程式都是在linux執行的,在windows的code::blocks有錯誤,偷懶就先用雲端環境練習程式。(Repl.it)
windows可以下載Windows 10 - Sysinternals Process Explorer Tool Usage觀看process

Deadlock

參考:Java Thread Deadlock 執行緒 死鎖
[系列文目錄] 作業系統 Operating System Concepts

synchronized

同步?
[Java] Synchronized 的四種用法,讀懂這篇就夠了
使用-synchronized-來解決multi-thread中共享變

程式:沒有synchronized:

class Main {
 static private int num = 0;
 static Object obj1 = new Object();
 //Object obj2 = new Object();
 static class ThreadTestAdd implements Runnable {
  public void run() {
   // synchronized(obj1) {  
   for (int i = 0; i < 3; i++) {
    System.out.println(Thread.currentThread().getName() + "num:" + ++num);
   }
   // }  
  }
 }
 static class ThreadTestSub implements Runnable {
  public void run() {
   //  synchronized(obj1) {  
   for (int i = 0; i < 3; i++) {
    System.out.println(Thread.currentThread().getName() + "num:" + --num);
   }
   // }  
  }
 }


 public static void main(String[] args) {
  ThreadTestAdd t1 = new ThreadTestAdd();
  ThreadTestSub t2 = new ThreadTestSub();
  Thread ta = new Thread(t1, "A");
  Thread tb = new Thread(t2, "B");
  ta.start();
  tb.start();
 }
}

結果:

Anum:1
Anum:1
Anum:2
Bnum:0
Bnum:1
Bnum:0

程式:有synchronized:

class Main {
 static private int num = 0;
 static Object obj1 = new Object();
 //Object obj2 = new Object();
 static class ThreadTestAdd implements Runnable {
  public void run() {
   synchronized(obj1) {
    for (int i = 0; i < 3; i++) {
     System.out.println(Thread.currentThread().getName() + "num:" + ++num);
    }
   }
  }
 }
 static class ThreadTestSub implements Runnable {
  public void run() {
   synchronized(obj1) {
    for (int i = 0; i < 3; i++) {
     System.out.println(Thread.currentThread().getName() + "num:" + --num);
    }
   }
  }
 }


 public static void main(String[] args) {
  ThreadTestAdd t1 = new ThreadTestAdd();
  ThreadTestSub t2 = new ThreadTestSub();
  Thread ta = new Thread(t1, "A");
  Thread tb = new Thread(t2, "B");
  ta.start();
  tb.start();
 }
}

結果:

Anum:1
Anum:2
Anum:3
Bnum:2
Bnum:1
Bnum:0

2 Java ByteArrayOutputStream

來源:Java ByteArrayOutputStream
Java.io.ByteArrayOutputStream.toString() Method
緩衝Bytes 好用的class ByteArrayOutputStream (JAVA)

ByteArrayOutputStream最後要寫flush();close();
為什麼要寫flush()?揭开Java IO流中的flush()的神秘面纱

了解byte這個單位:
Java 基本数据类型

練習:

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayInputStream_TEST01 {
 public static void main(String[] args) throws IOException {
  String str = "";
  String hi = "嗨世界";
  ByteArrayOutputStream baos = null;
  try {
   baos = new ByteArrayOutputStream();
   baos.write(hi.getBytes());

   //ByteArrayOutputStream轉成位元陣列
   byte data[] = baos.toByteArray();

   for (int i = 0; i < data.length; i++) {
    System.out.print(data[i] + ",");
   }
   System.out.println();

   //Cp1047
   str = baos.toString("Cp1047");
   System.out.println(str);

   //UTF-8
   str = baos.toString("UTF-8");
   System.out.println(str);

   //ASCII
   str = baos.toString("ASCII");
   System.out.println(str);

  } catch (Exception e) {
   // if I/O error occurs
   e.printStackTrace();
  } finally {
   if (baos != null) {
    baos.flush();
    baos.close();
   }
  }
 }
}

結果:

-27,-105,-88,-28,-72,-106,-25,-107,-116,
VpyU½oXnð
嗨世界
���������

3

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (4128x3096, max=4096x4096)
這個問題好像是 圖片的像素> imageview能夠容納的最大像素。所以顯示不了圖片
參考:解答3

4

有時候logcat會看不到訊息:
Android studio logcat nothing to show
https://ithelp.ithome.com.tw/upload/images/20191020/20111994hSeIx6YQ06.png
檢查項目:
1 檢查裝置和package,是不是disconnect或dead
2 log的種類,比較高的種類,像是error就只能看到error的log
3 搜尋關鍵字
4 show only selected application:應該是:只會顯示你選擇的裝置和package的訊息,不會顯示其他正在執行的裝置訊息
5 重整

5

如何了解Bitmap、View、Drawable、Canvas以及Paint的差異


上一篇
Android, camera(續) , SQLite, java判斷圖片檔
下一篇
Android 畫面、delete image、Apache(php) , 安裝apache,import library
系列文
練習程式37

尚未有邦友留言

立即登入留言