在Android中将图像加载为静态服务器

huangapple go评论86阅读模式
英文:

Loading image in Android as static server

问题

我正在尝试在我的Android应用程序中运行GO服务器,该应用程序正在运行相机,并将文件保存在以下位置:

file:///storage/emulated/0/Android/media/com.myapp/

我试图将此路径定义为服务器的静态路径,如下所示:

fsAndroid := http.FileServer(http.Dir("file:///storage/emulated/0/Android/media/com.myapp/"))
http.Handle("/android/", fsAndroid)

	go func() {
		log.Println(http.ListenAndServe("127.0.0.1:6060", nil))
		<-c
	}()

但是,一旦我尝试打开从相机中保存的文件,并访问:

http://127.0.0.1:6060/android/myfile.jpg

我得到了404页面未找到的错误。

有什么想法吗?

英文:

I'm tryig to run GO server in my Android app, the app is running the camera,and saving files at:

file:///storage/emulated/0/Android/media/com.myapp/

I'm trying to define this bath as the static path in my server, as:

fsAndroid := http.FileServer(http.Dir(&quot;file:///storage/emulated/0/Android/media/com.myapp/&quot;))
http.Handle(&quot;/android/&quot;, fsAndroid)

	go func() {
		log.Println(http.ListenAndServe(&quot;127.0.0.1:6060&quot;, nil))
		&lt;-c
	}()

But once I tried to open the files saved there, after being taken from the camera, and I go to:

http://127.0.0.1:6060/android/myfile.jpg

I'm getting the 404 page not found

Any thought?

答案1

得分: 0

我解决了,并分享了我的文件:这里

  1. 在Go中,我使用了上面fizzie提供的提示:
	fsAndroid := http.FileServer(http.Dir("/storage/emulated/0/")) // Pictures/tk.cocoon/

	http.Handle("/android/", http.StripPrefix("/android/", fsAndroid))
  1. 在Android中,我使用了:
interface ImageUri {
    companion object {
        fun create() : Uri? {
            // 创建图像文件名
            val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
            Global.fileName = "IMG_${timeStamp}.jpg"
            Global.filePath = "Pictures/tk.cocoon/IMG_${timeStamp}.jpg"

            val resolver = Global.context.contentResolver
            val contentValues = ContentValues().apply {
                put(MediaStore.MediaColumns.DISPLAY_NAME, Global.fileName)
                put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
                //put(MediaStore.MediaColumns.RELATIVE_PATH, "Android/media/tk.cocoon/")
                put(MediaStore.MediaColumns.RELATIVE_PATH, "Pictures/tk.cocoon/")
            }
            Global.photoURI = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
            return Global.photoURI
        }
    }
}
英文:

I solved it, and sharing my files: here

  1. From Go, I used, thanks for fizzie note above:
	fsAndroid := http.FileServer(http.Dir(&quot;/storage/emulated/0/&quot;)) // Pictures/tk.cocoon/

	http.Handle(&quot;/android/&quot;, http.StripPrefix(&quot;/android/&quot;, fsAndroid))
  1. In Android, I used:
interface ImageUri {
    companion object {
        fun create() : Uri? {
            // Create an image file name
            val timeStamp = SimpleDateFormat(&quot;yyyyMMdd_HHmmss&quot;, Locale.getDefault()).format(Date())
            Global.fileName = &quot;IMG_${timeStamp}.jpg&quot;
            Global.filePath = &quot;Pictures/tk.cocoon/IMG_${timeStamp}.jpg&quot;

            val resolver = Global.context.contentResolver
            val contentValues = ContentValues().apply {
                put(MediaStore.MediaColumns.DISPLAY_NAME, Global.fileName)
                put(MediaStore.MediaColumns.MIME_TYPE, &quot;image/jpeg&quot;)
                //put(MediaStore.MediaColumns.RELATIVE_PATH, &quot;Android/media/tk.cocoon/&quot;)
                put(MediaStore.MediaColumns.RELATIVE_PATH, &quot;Pictures/tk.cocoon/&quot;)
            }
            Global.photoURI = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
            return Global.photoURI
        }
    }
}

huangapple
  • 本文由 发表于 2021年9月3日 05:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/69037086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定