2012-02-17 12 views
0

私はイメージで作業しています。私のアプリケーションでは、drawableから画像を表示し、そのdrawable画像をImageViewに設定しました。ボタンをクリックすると、Base64を使ってImageViewイメージをバイトコードにエンコードしたいと思います。Base64エンコードを使用してImageViewイメージをバイトコードに変換するには?

次のように私は、コードを実装している:

((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.person); 

((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     ((TextView)findViewById(R.id.textView1)).setText("Get((ImageView)findViewById(R.id.imageView1)) image Base64.encode() here"); 
    } 
}); 

私はバイトコードにエンコードimageView1の画像を取得できますか?

誰でも助けてください。このコードでは

答えて

3

は今

tv.setText(img_str); 
としてあなたのTextViewにその文字列を設定し

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images); 
    ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
    byte[] image=stream.toByteArray(); 
    System.out.println("byte array:"+image); 

    String img_str = Base64.encodeToString(image, 0); 
    System.out.println("string:"+img_str); 

...これを試してみてください

+0

Base64にエンコードしている間は常に画像を圧縮する必要がありますか?イメージ/イメージファイルを圧縮せずにBase64でエンコードされた文字列に直接変換することは可能ですか? – VikramV

1

使用この

public String encode(Bitmap icon) { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     icon.compress(Bitmap.CompressFormat.PNG, 50, baos); 
     byte[] data = baos.toByteArray(); 
     String test = Base64.encodeBytes(data); 
     return test; 
    }` 
+0

imageView1をどのようにBitmapで取得できますか? –

+0

ビットマップアイコン= BitmapFactory.decodeResource(context.getResources()、 R.drawable.person); –

1

見て、

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.person) 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bMap .compress(Bitmap.CompressFormat.PNG, 100, baos); 
//bMap is the bitmap object 
byte[] b = baos.toByteArray(); 
String encodedString = Base64.encodeToString(b, Base64.DEFAULT) 
関連する問題