2011-08-17 6 views
0

現在、私はkSoapを使用してC#Webサービスにデータを公開しています。今では、Base64Binaryを使用して自分のマシンから画像をアップロードする必要がある部分に来ました。私はeverywehreインターネットを検索しましたが、適切な解決策を考え出すことができませんでした。Android:Base64サンプルの問題

例はexternal Base64 classですが、ネイティブソリューションに興味があります(Android 2.2のthere is a API)。

私は初心者なので自分でできませんでした。基本的に私は画像のSDカードファイルのパスを持って、私はBase64形式にアップロードするに変換したい。

誰かが私を助けたり、適切な書類を私に案内してくれることを願っています。

ありがとうございます。

答えて

0

を使用して画像にベース64文字列をデコードすることができ、私は

String Image64 = null; 


     try { 
      Image64 = Base64.encodeToString((getBytesFromFile(new File(path))), DEFAULT); 

     } catch (IOException e) { 
      e.printStackTrace(); 

     } 

//encording 
    public static byte[] getBytesFromFile(File file) throws IOException { 
     InputStream is = new FileInputStream(file); 

     // Get the size of the file 
     long length = file.length(); 

     // You cannot create an array using a long type. 
     // It needs to be an int type. 
     // Before converting to an int type, check 
     // to ensure that file is not larger than Integer.MAX_VALUE. 
     if (length > Integer.MAX_VALUE) { 
      // File is too large 
     } 

     // Create the byte array to hold the data 
     byte[] bytes = new byte[(int)length]; 

     // Read in the bytes 
     int offset = 0; 
     int numRead = 0; 
     while (offset < bytes.length 
       && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
      offset += numRead; 
     } 

     // Ensure all the bytes have been read in 
     if (offset < bytes.length) { 
      throw new IOException("Could not completely read file "+file.getName()); 
     } 

     // Close the input stream and return bytes 
     is.close(); 
     return bytes; 
    } 
0

これを試してください。指定したリンクからBase64.javaを使用してください。

Bitmap bmImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "Your filename"); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
String encodedString = Base64.encodeBytes(baos.toByteArray()); 

あなたは、ファイルの種類に応じて、このBitmap.CompressFormat.JPEG文の拡張子を変更する必要があります。次のコード

byte[] b; 
b = Base64.decode("base 64 string"); 
final Bitmap unscaledBitmap = BitmapFactory.decodeByteArray(b, 0, b.length); 
+0

が、これは役に立たない問題を考え出しましたか? – Sandy

+0

申し訳ありませんでした。私が言ったように、私はネイティブソリューションを望んでいました。 –

関連する問題