英文:
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("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
	}()
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
我解决了,并分享了我的文件:这里
- 在Go中,我使用了上面fizzie提供的提示:
 
	fsAndroid := http.FileServer(http.Dir("/storage/emulated/0/")) // Pictures/tk.cocoon/
	http.Handle("/android/", http.StripPrefix("/android/", fsAndroid))
- 在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
- From Go, I used, thanks for fizzie note above:
 
	fsAndroid := http.FileServer(http.Dir("/storage/emulated/0/")) // Pictures/tk.cocoon/
	http.Handle("/android/", http.StripPrefix("/android/", fsAndroid))
- In Android, I used:
 
interface ImageUri {
    companion object {
        fun create() : Uri? {
            // Create an image file name
            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
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论