今アンドロイドアプリを作成しています。
ユーザーがプロフィール画像を設定できるようにしたいです。
bitmapを取得しimagebuttonにセットすることをやりたいのですが、やり方がわからなくて困っています。
アクティビティに書くべきなのでしょうか?もしそうならなんて書けば良いのでしょうか?
AsyncTaskActivity.java には
Java
1package me.myreco.up; 2 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.graphics.drawable.BitmapDrawable; 6import android.os.AsyncTask; 7import android.net.Uri; 8import android.support.v7.widget.AppCompatImageButton; 9import android.widget.ImageButton; 10import android.widget.ImageView; 11 12 13 14public class AsyncTaskTestActivity extends AsyncTask<Uri.Builder, Void, Bitmap>{ 15 16 private Bitmap bitmap; 17 18 19 20 @Override 21 protected Bitmap doInBackground(Uri.Builder... builder){ 22 23 // 受け取ったbuilderでインターネット通信する 24 // 後で入力する 25 26 ImageView imageView = (ImageView)findViewBy(R.id.imageButton2) ; 27 Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); 28 return bitmap; 29 }
}
と記載し、
protected Bitmap doInBackground(Uri.Builder... builder){ // 受け取ったbuilderでインターネット通信する // 後で入力する ImageView imageView = (ImageView)findViewBy(R.id.imageButton2) ; Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); return bitmap; }
のところでbitmapを取得するようにしました。
Activityの方では
Java
1 @Override 2 public void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.profile_activity); 5 6 FirebaseAnalytics = FirebaseAnalytics.getInstance(this); 7 FirebaseAnalytics.logEvent("LoginFragment", null); 8 9 10 mailEdit = (EditText) findViewById(R.id.editingusername); 11 usernametext = (EditText) findViewById(R.id.editingmail); 12 passEdit = (EditText) findViewById(R.id.editingpass); 13 introductiontext = (EditText) findViewById(R.id.editingintro); 14 areatext = (EditText) findViewById(R.id.editingarea); 15 birthdaytext = (EditText) findViewById(R.id.editingbirthday); 16 sextext = (EditText) findViewById(R.id.editingsex); 17 twittertext = (EditText) findViewById(R.id.editingtwitter); 18 facebooktext = (EditText) findViewById(R.id.editingfacebook); 19 icon = (ImageButton) findViewById(R.id.imageButton2); 20 21 22 View.OnTouchListener focus_listener = new View.OnTouchListener() { 23 @Override 24 public boolean onTouch(View v, MotionEvent event) { 25 v.requestFocusFromTouch(); 26 return false; 27 } 28 }; 29 30 mailEdit.setOnTouchListener(focus_listener); 31 32 passEdit.setOnTouchListener(focus_listener); 33 34 usernametext.setOnTouchListener(focus_listener); 35 36 introductiontext.setOnTouchListener(focus_listener); 37 38 areatext.setOnTouchListener(focus_listener); 39 40 birthdaytext.setOnTouchListener(focus_listener); 41 42 sextext.setOnTouchListener(focus_listener); 43 44 twittertext.setOnTouchListener(focus_listener); 45 46 facebooktext.setOnTouchListener(focus_listener); 47 48 saveButton = (Button) findViewById(R.id.editsave); 49 50 SavePreference savePreference = new SavePreference(); 51 String aToken = savePreference.load_aToken(getApplicationContext()); 52 53 UserInfoApi api = ServiceGenerator.createService(UserInfoApi.class, aToken); 54 Call<UserInfo> call = api.getUserInfo(); 55 56 call.enqueue(new Callback<UserInfo>() { 57 58 public void onResponse(Call<UserInfo> call, Response<UserInfo> UserInfoResponse) { 59 String email = UserInfoResponse.body().getEmail(); 60 Integer id = UserInfoResponse.body().getId(); 61 String username = UserInfoResponse.body().getUsername(); 62 Object area = UserInfoResponse.body().getArea(); 63 String bio = UserInfoResponse.body().getAttribute(); 64 String sex = UserInfoResponse.body().getSex(); 65 Object birthday = UserInfoResponse.body().getBirthday(); 66 String iconurl = (String) UserInfoResponse.body().getIcon(); 67 68 69 mailEdit.setText(email); 70 71 passEdit.setText(String.valueOf(id)); 72 if (username != null) { 73 usernametext.setText(String.valueOf(username)); 74 } 75 if (area != null) { 76 areatext.setText(String.valueOf(area)); 77 } 78 if (area != null) { 79 areatext.setText(String.valueOf(area)); 80 } 81 if (bio != null) { 82 introductiontext.setText(String.valueOf(bio)); 83 } 84 if (sex != null) { 85 sextext.setText(String.valueOf(sex)); 86 } 87 if (birthday != null) { 88 birthdaytext.setText(String.valueOf(birthday)); 89 } 90 if (iconurl != null) { 91 URL url = null; 92 try { 93 url = new URL(iconurl); 94 } catch (MalformedURLException e) { 95 e.printStackTrace(); 96 } 97 InputStream istream = null; 98 try { 99 istream = url.openStream(); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } 103 //読み込んだファイルをビットマップに変換 104 Bitmap oBmp = BitmapFactory.decodeStream(istream); 105 //ビットマップをImageButtonに設定 106 bitmap = RadiusImage(bitmap); 107 icon.setImageDrawable(null); 108 icon.setImageBitmap(bitmap); 109 //インプットストリームを閉じる 110 try { 111 istream.close(); 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 116 } 117 } 118 119 public void onFailure(Call<UserInfo> call, Throwable t) { 120 new AlertDialog.Builder(getApplicationContext()) 121 .setTitle("失敗しました") 122 .setMessage("") 123 .setPositiveButton("OK", null) 124 .show(); 125 126 } 127 128 }); 129 130 131 saveButton.setOnClickListener(new View.OnClickListener() { 132 @Override 133 public void onClick(View v) { 134 135 } 136 }); 137 } 138 139 140 141 // ATTENTION: This was auto-generated to implement the App Indexing API. 142 // See https://g.co/AppIndexing/AndroidStudio for more information. 143 144 public Bitmap RadiusImage(Bitmap bm) { 145 int width = bm.getWidth(); 146 int height = bm.getHeight(); 147 int size = Math.min(width, height); 148 Bitmap clipArea = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 149 Canvas c = new Canvas(clipArea); 150 c.drawRoundRect(new RectF(0, 0, size, size), size, size, new Paint(Paint.ANTI_ALIAS_FLAG)); 151 Bitmap newImage = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 152 Canvas canvas = new Canvas(newImage); 153 Paint paint = new Paint(); 154 canvas.drawBitmap(clipArea, 0, 0, paint); 155 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 156 canvas.drawBitmap(bm, new Rect(0, 0, size, size), new Rect(0, 0, size, size), paint); 157 return bitmap; 158 } 159 160}
と記載しました。
Java
1 if (iconurl != null) { 2 URL url = null; 3 try { 4 url = new URL(iconurl); 5 } catch (MalformedURLException e) { 6 e.printStackTrace(); 7 } 8 InputStream istream = null; 9 try { 10 istream = url.openStream(); 11 } catch (IOException e) { 12 e.printStackTrace(); 13 } 14 //読み込んだファイルをビットマップに変換 15 Bitmap oBmp = BitmapFactory.decodeStream(istream); 16 //ビットマップをImageButtonに設定 17 bitmap = RadiusImage(bitmap); 18 icon.setImageDrawable(null); 19 icon.setImageBitmap(bitmap); 20 //インプットストリームを閉じる 21 try { 22 istream.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 27 }
のところで該当処理をしています。
お願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/08/25 06:00
2016/08/25 07:47
退会済みユーザー
2016/08/25 12:21
2016/08/25 12:27
退会済みユーザー
2016/08/25 13:05
2016/08/25 13:12
退会済みユーザー
2016/08/25 14:10
2016/08/25 14:53