2013-08-19 61 views
0

從互聯網上使用這個流行的代碼:圖片不顯示 - 從攝像頭的圖像確實

代碼

<input type="file" id="files" name="files[]" multiple="multiple" /> 
<output id="list"></output> 
<script> 
    function handleFileSelect(evt) { 
     var files = evt.target.files; // FileList object 

     // Loop through the FileList and render image files as thumbnails. 
     for (var i = 0, f; f = files[i]; i++) { 

      // Only process image files. 
      if (!f.type.match('image.*')) { 
       continue; 
      } 

      var reader = new window.FileReader(); 

      // Closure to capture the file information. 
      reader.onload = (function (theFile) { 
       return function (e) { 
        // Render thumbnail. 
        var span = document.createElement('span'); 
        span.innerHTML = ['<img class="thumb" src="', e.target.result, 
             '" title="', escape(theFile.name), '"/>'].join(''); 
        document.getElementById('list').insertBefore(span, null); 
       }; 
      })(f); 

      // Read in the image file as a data URL. 
      reader.readAsDataURL(f); 
     } 
    } 

    document.getElementById('files').addEventListener('change', handleFileSelect, false); 
</script> 

在我的Android平板電腦拍照時的圖像只顯示使用相機而不是從文件系統中選擇圖像。

在我的筆記本電腦上(我只能從文件系統中選擇文件)顯示圖像。

這裏有沒有一些安全問題?

使用警報來檢查腳本在android設備上運行的距離,我可以看到腳本運行良好,沒有任何錯誤。

Android設備運行的是Android版本4.2.1

回答

0

請試試這個代碼,你我有跟蹤問題。

<!DOCTYPE html> 
    <html> 
     <head> 
     </head> 
     <body> 
      <input type="file" id="files" name="files[]" multiple="multiple" /> 
      <output id="list"> 
      </output> 
     </body> 
     <script> 
      function handleFileSelect(evt) { 
       var files = evt.target.files; // FileList object 
       // Loop through the FileList and render image files as thumbnails. 
       for (var i = 0, f; f = files[i]; i++) { 
        console.log("for call"); 

        var reader = new window.FileReader(); 

        // Closure to capture the file information. 
        reader.onload = (function(theFile) { 
         return function(e) { 
          // Render thumbnail. 
          var span = document.createElement('span'); 
          console.log(e.target.result); 
          var spl = e.target.result.split(","); 
          console.log("data:image/png;base64," + spl[1]); 
          var el = "data:image/png;base64," + spl[1]; 

          span.innerHTML = ['<img class="thumb" src="', el, '" title="', escape(theFile.name), '"/>'].join(''); 
          document.getElementById('list').insertBefore(span, null); 
         }; 
        })(f); 

        // Read in the image file as a data URL. 
        reader.readAsDataURL(f); 
       } 
      } 

      document.getElementById('files').addEventListener('change', handleFileSelect, false); 
     </script> 

    </html> 
+0

哇!有用。 什麼.... 所以Android不設置MIME類型? –

+0

是@Jakob B你試試這個 –