先修改 app/src/main/AndroidManifest.xml 檔案內容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.urlopen">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:networkSecurityConfig="@xml/network_security"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UrlOpen">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在 res 目錄底下新建一個 xml 目錄( app/src/main/res/xml), 裏面放入 netwirk_security.xml 檔案內容, 以便信認所屬網域 192.168.0.1:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.0.1</domain>
</domain-config>
</network-security-config
修改 app/src/main/res/layout/activity_main.xml, 加入 android:id="@+id/...", 檔案內容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout"
tools:context=".MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
主程式 app/src/main/java/com/example/urlopen/MainActivity.kt 檔案內容:
package com.example.urlopen
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import java.net.URL
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mainContext = this
(findViewById(R.id.mainLayout) as ConstraintLayout) ?.apply {
val imageView = ImageView(mainContext).apply {
setLayoutParams(
ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
)
}
addView(imageView)
val url = "http://192.168.0.1/snap.jpg"
val handler = Handler(Looper.getMainLooper())
val getJPG = object : Runnable {
override fun run() {
Executors.newSingleThreadExecutor() ?.execute {// doInBackground
try {
val stream = URL(url).openStream()
val jpg = BitmapFactory.decodeStream(stream)
handler.post { // onPostExecute
imageView.setImageBitmap(jpg)
}
} catch (e: Exception) {
Log.e("JNI", "network get fail!")
}
}
}
}
getJPG.run()
}
//...
}
}
沒有留言:
張貼留言