2016-10-01 108 views
3

我已經編寫代碼將圖像從文件位置轉換爲base64。我可以很容易地從絕對文件位置轉換爲base64像:C:/用戶/ Java工程師/桌面/測試/畫廊/ magar/Kanuglam.jpg,但我不能轉換位置像
enter image description here 。我想要從web服務轉換爲Android使用的圖像。 下面是代碼示例:如何將Uri圖像轉換爲Base64?

/** 
* TEST JSON 
*/ 
String convertToJsonArrayWithImageForMovieDetailTest(ResultSet rs) { 

    System.out.println("I am insied json converter"); 
    JSONArray list = new JSONArray(); 
    JSONObject obj ; 

    //File file; 
    File locatedFile; 
    FileInputStream fileInputStream; 
    try { 

     while (rs.next()) { 
      obj = new JSONObject(); 
      System.out.println("inside RS"); 
      System.out.println("date is there ha ha "); 

      obj.put("movie_name", rs.getString("name")); 
      obj.put("movie_gener", rs.getString("type")); 
      String is_free_stuff = rs.getString("is_free_stuff"); 
      if (is_free_stuff == "no") { 
       is_free_stuff = "PAID"; 
      } else { 
       is_free_stuff = "FREE"; 
      } 
      obj.put("movie_type", is_free_stuff); 

      //String movie_image = rs.getString("preview_image"); 

      //this does not work 
      String movie_image = "http://www.hamropan.com/stores/slider/2016-09-10-852311027.jpg"; 

      //this works for me 
     // file = new File("C:/Users/Java Engineer/Desktop/Nike Zoom Basketball.jpg"); 
      locatedFile = new File(movie_image); 
      // Reading a Image file from file system 
      fileInputStream = new FileInputStream(locatedFile); 
      if (locatedFile == null) { 
       obj.put("movie_image", "NULL"); 
      } else { 
       byte[] iarray = new byte[(int) locatedFile.length()]; 
       fileInputStream.read(iarray); 
       byte[] img64 = com.sun.jersey.core.util.Base64 
         .encode(iarray); 
       String imageString = new String(img64); 
       obj.put("movie_image", imageString); 
      } 
      list.add(obj); 
     } 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return list.toString(); 
} 

的代碼塊對我的作品,但它似乎慢

public String imageConvertMethod(String url) throws Exception{ 
    ByteArrayOutputStream output = new ByteArrayOutputStream(); 

    try (InputStream input = new URL(url).openStream()) { 
     byte[] buffer = new byte[512]; 
     for (int length = 0; (length = input.read(buffer)) > 0;) { 
      output.write(buffer, 0, length); 
     } 
    } 

    byte [] byte_array = output.toByteArray(); 

    byte[] img64 = com.sun.jersey.core.util.Base64 
      .encode(byte_array); 
    String imageString = new String(img64); 

    return imageString; 
} 
+0

你必須先下載文件。 – greenapps

+0

廢話。你的帖子告訴別的。 – greenapps

+0

我的帖子裏說了什麼? @greenapps – javalok

回答

1

好吧,從URL試試這個

bitmap = getBitmapFromUrl(image_url); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

byte[] array = stream.getByteArray(); 
encoded_string = Base64.encodeToString(array, 0); 

方法を載圖片

public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
    e.printStackTrace(); 
    return null; 
    } 
} 
+0

在後臺線程上運行 –

+0

我需要獲取圖像相對路徑,如:/abc/cba/a.jpg並將其轉換爲字符串並將其發送給android應用程序。當我使用c:/abc/cba/a.jpg它可以工作,但/abc/cba/a.jpg不工作。我使用位於D:dir中的web服務(jersey api),但圖像是在C:所以,我想從我的網絡服務應用程序目錄中的圖像到另一個相對目錄。我嘗試使用Url但它太慢,所以需要另一種選擇。謝謝 – javalok

+0

感謝所有試圖幫助我的朋友,但我強制使用另一種想法,在那裏,我將數據庫中的圖像保存爲blob並將其保存到Android應用程序中。 – javalok