Detailed explanation of HTML5 image upload examples in Android
Application platform environment: Android webview;
Technical points involved:
(1) : During development, Android webview clicks by default File selection and camera photography cannot be called (other device ios and other browsers do not have this problem), and Android development students need to adjust the code;
(2) How to display the image on the page after clicking to select it? ? Many developers probably get this.value directly, which is wrong and not feasible. There are two APIs that can be implemented, new FileReader and window.URL.createObjectURL (optimal);
The implementation of new FileReader is as follows:
upload
In actual Android webview combat, the reading and writing speeds are quite slow and are not recommended;
//-------------- -------------------------------------------------- ------------------------
window.URL.createObjectURL is implemented as follows:
<input type="file" name="" id="j-file"> <img src="" id='j-img' alt="" width="100%"> <button id='j-btn'>upload</button> <script type="text/javascript"> var o_file = document.getElementById('j-file'), o_btn = document.getElementById('j-btn'), o_img = document.getElementById('j-img'); o_file.addEventListener('change',function(event){ var file = event.target.files[0]; if(!file) return; var url = window.URL.createObjectURL(file); if(/image/.test(file.type)){ o_img.setAttribute('src',url); }else{ console.log('请选择图片'); } },false); o_btn.onclick = function(){ var data = new FormData(); data.append('file_a',o_file.files[0]); data.append('text','test'); console.log(data); } </script>
#The advantage of this technology is that you don’t have to read the file content into javascript, but use the file content directly, which is very fast;
(3) How to pass image data to the server through the XHR object? new FormData() provides an implementation for serializing forms and creating data in the same format as the form (XHR transmission). FormData does not need to explicitly set the request header on the xhr object (if it is an image upload in the form, enctype needs to set multipart/form -data), XHR can automatically identify the instance method .append(key, value) to customize the data;
The complete code example is as follows:
<input type="file" name="" id="j-file"> <img src="" id='j-img' alt="" width="100%"> <button id='j-btn'>upload</button> <script type="text/javascript"> var o_file = document.getElementById('j-file'), o_btn = document.getElementById('j-btn'), o_img = document.getElementById('j-img'), target_file = null; o_file.addEventListener('change',function(event){ var file = event.target.files[0]; if(!file) return; target_file = file; var url = window.URL.createObjectURL(target_file); if(/image/.test(target_file.type)){ o_img.setAttribute('src',url); }else{ console.log('请选择图片'); } },false); o_btn.onclick = function(){ if(!target_file) return; //数据处理 var data = new FormData(); data.append('key',target_file); var xhr = new XMLHttpRequest(); if(xhr.upload){ xhr.upload.addEventListener("progress", function(e){ var loaded = e.loaded; //已经上传大小情况 var tot = e.total; //附件总大小 var per = Math.floor(100*loaded/tot); //已经上传的百分比 console.log(per+'%');//进度 }, false); } xhr.onreadystatechange = function(e) { if (xhr.readyState == 4) {if (xhr.status >=200&&xhr.status<300||xhr.status==304) {//上传成功 } } }; xhr.onloadend = function(){//无论失败或成功 } xhr.onerror = function(){ //网络失败 } // 开始上传 xhr.open("POST",'上传地址', true); xhr.send(data); } </script>
The above is the detailed content of Detailed explanation of HTML5 image upload examples in Android. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on June 28, the source @ibinguniverse tweeted that the Samsung Galaxy S25 Ultra mobile phone will no longer pinch the hands, and the corners will be rounded, making it easier for users to hold it for a long time. Fenye stated in its Weibo content that the body R angle of the Samsung Galaxy S25 Ultra mobile phone can refer to the Galaxy Note7 mobile phone. The attached picture is as follows: The source also said: Galaxy S25 Ultra has a body thickness of 8.4mm, bucking the trend and becoming thinner, making it the thinnest among SUltra models in recent years. S21U to S23U are all 8.9mm, and S24U is 8.6mm. Samsung's design still doesn't want to compromise with the image, and it doesn't want to develop in the direction of a bunch of Oreos. Fenye

According to news on May 17, Android engineer Mishaal Rahman recently published a blog post and found that Android 15Beta2 has adjusted the storage settings and subdivided the "System" into system files and temporary files. Note: Android users currently open storage settings and can see the space occupied by different types of files, including applications, documents, pictures, games, audio, etc. There is also a "System" space that displays the storage space occupied by basic system files. In the Android 15 system, in addition to seeing the space occupied by the above file types, System is further subdivided into OS (specific Android version) and system temporary files. Users can see temporary files more intuitively

Thanks to netizen Wu Yanzu from South China for submitting the clue! According to news on July 6, AYANEO company released on Weibo last night, previewing the launch of a new Pocket EVO product, claiming to be the world's first flagship Android handheld device equipped with a 7-inch 1080 POLED 120Hz high-brush color screen. The main selling point of AYANEOPocketEVO is the use of OLED screens, which provide more delicate colors and faster response speeds, bringing players high-scoring, high-frame-rate gaming and audio-visual entertainment experiences. fenyeAYANEO gaming handheld 60Hz refresh rate: AYANEOMicro: 3.5-inch 960x640 pixels IPSLCD display AYANEOPocketS: 6-inch 1920x1080 or 25

According to news on June 21, well-known digital blogger Digital Chat Station revealed that the battery capacity of iQOO’s flagship phone iQOO13 this year will be 6000mAh. It is reported that the latest iQOO12 series model has a battery capacity of 5000mAh. Thanks to the development and commercialization of carbon-silicon anode battery technology this year, the iQOO13 to be released in the second half of this year will benefit from the battery technology, and the capacity will directly increase from 5000mAh. To 6000mAh, it is thin and light while taking into account long battery life. In addition, according to the blogger, the iQOO13 series will use the Snapdragon 8Gen4 mobile platform, inherit 3X periscope telephoto, add a single-point ultrasonic under-screen fingerprint, and will also support IP68 dustproof and waterproof, built-in oversized X-axis linear

July 25 news: According to foreign media reports, Apple may enable larger storage on the 2026 iPhone, which is expected to be 2TB. The latest revelation from the industry chain is that Apple is accelerating the replacement of QLCNAND, thus raising the upper limit of built-in storage to 2TB. As for why QLC is used, it may be related to Apple's cost control. In fact, there was news before this that Apple may change the storage capacity (enabled on iPhone 16) and no longer use three-layer cell (TLC) NAND flash memory, but use four-layer cells on models with a storage capacity of 1TB or more. (QLC) NAND flash memory. Compared with TLC, the advantage of QLC is that each memory cell can store four bits of data.

According to the latest news on May 13, Huawei will fully switch to its own platform HarmonyOS in the second half of this year and abandon the Android system. The report mentioned that the next version of HarmonyOS is expected to be released with Huawei’s upcoming Mate70 flagship series. It is reported that HarmonyOSNext has expanded to 4,000 applications, including Alipay and McDonald's. According to sources, the total number of HarmonyOSNext applications will increase to 5,000 within the year, and Huawei's goal is to reach 500,000 in the short term. HarmonyOSNext is designed from scratch and the operating system is independently developed by Huawei. Although this set of code was originally written for IoT devices,

According to news on August 8, today digital blogger @ Smart Pikachu revealed that Nubia’s new true full-screen flagship will continue to be equipped with quasi-1.5K under-screen front-facing technology, and is expected to be the Nubia Z70 Ultra equipped with the Snapdragon 8Gen4 mobile platform. . The true full-screen design of the Nubia Z series has been committed to the true full-screen design since the Nubia Z50 Ultra. Its follow-up product, the Z60 Ultra leading version, also inherits this concept. Quasi-1.5K resolution The screen resolution of these models has reached 2480x1116, which is between 1.5K and 1080P resolution, and can be called quasi-1.5K. Under-screen proactive technology Although there was news last year about ZTE’s development of 1.5K under-screen proactive technology, considering the length of the research and development cycle, currently

According to news on June 28, overseas retailers recently released high-definition renderings of Samsung Galaxy Z Flip 6 in advance. 1. Samsung Galaxy ZFlip6 comes in four colors: green, silver, blue and yellow. It is expected to be the same as ZFlip5 and is exclusive to Samsung Mall. According to the picture, the frame design of ZFlip6 will be narrower, and the width of the phone will be reduced when folded, bringing a more comfortable grip and portability. ZFlip6 has a more square shape, and the camera module on the back is significantly raised, indicating an upgrade of the camera sensor. In terms of performance configuration, ZFlip6 will be equipped with Snapdragon 8Gen3, 8GB/12GB memory and 256GB/512GB storage space. Samsung has officially announced that the ZFlip6 series will be officially released on July 10.
