2011-11-18 53 views
-4

可能重複保存圖片到三星,LG,諾基亞手機的相冊:
How to save an image into photo gallery using j2me如何使用J2ME

我上的應用程序的工作中,我想下載從互聯網上的圖像,並將其保存到每個手機的照片庫。請提供我的建議。 謝謝,

+0

爲什麼你發佈相同的問題? – Lucifer

+2

訪問此[鏈接] [1]爲您的答案。 [1]:http://stackoverflow.com/questions/8150418/how-to-save-an-image-into-photo-gallery-using-j2me – Lucifer

+0

也期待在這次討論中,[如何下載圖像](http://stackoverflow.com/questions/7226245/how-to-download-images-from-a-computer-webserver-to-a-phone-mobile) – bharath

回答

1
private void downloadImage(String folder, String photoName, String url) throws IOException 
    { 
     byte[] rawImg = null; 
     try 
     { 
      String imageData = getDataFromUrl(url); 
      rawImg = imageData.getBytes(); 
      putPhotoToPhone(rawImg, folder, photoName); 
     } 
     catch(Exception e1) { 
      e1.printStackTrace(); 
     } 
    } 

    private String getDataFromUrl(String url) throws IOException { 

     StringBuffer b = new StringBuffer(); 
     InputStream is = null; 
     HttpConnection c = null; 

     long len = 0 ; 
     int ch = 0; 
     c = (HttpConnection)Connector.open(url); 
     is = c.openInputStream(); 
     len = c.getLength(); 
     if(len != -1) 
     { 
      for(int i =0 ; i < len ; i++) 
      { 
       if((ch = is.read()) != -1) 
       { 
        b.append((char) ch); 
       } 
      } 
     } 
     else 
     { 
      while ((ch = is.read()) != -1) 
      { 
       len = is.available() ; 
       b.append((char)ch); 
      } 
     } 
     is.close(); 
     c.close(); 
     return b.toString(); 
    } 
    private void putPhotoToPhone(byte[] rawImg, String photoDir, String imageName) 
    { 
     FileConnection fcDir, fcFile; 
     String pRoot = "Phone:/"; 
     OutputStream os; 
     if (rawImg != null) 
     { 
      try 
      { 
       fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/", Connector.READ_WRITE); 
       if (!fcDir.exists()) 
        fcDir.mkdir(); 
       fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/"+imageName, Connector.READ_WRITE); 
       if (fcFile.exists()) 
        fcFile.delete(); 
       fcFile.create(); 
       os = fcFile.openOutputStream(); 
       os.write(rawImg); 
       os.flush(); 
       os.close(); 
       fcFile.close(); 
       fcDir.close(); 
      } 
      catch (Exception e) {} 
     } 
    } 
+0

謝謝,這是我所擁有的搜索。 – user1048972

+0

所以請點擊ckck-mark接受答案,並提出答案 – pheromix