public class MainActivity extends Activity implements OnClickListener
{
private static final int REQUEST_GALLERY = 0;
private ImageView imgView;
private Button btn1;
private Uri uri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.os.Debug.waitForDebugger();
imgView = (ImageView)findViewById(R.id.imgview_id);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
// ギャラリー呼び出し
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
try {
InputStream in = getContentResolver().openInputStream(data.getData());
Bitmap img = BitmapFactory.decodeStream(in);
uri = data.getData();
in.close();
// 選択した画像を表示
imgView.setImageBitmap(img);
} catch (Exception e) {
}
}
}
@Override
public void onClick(View v)
{
// ボタン押下時
if( v == btn1 )
{
Uri filePath = uri;
String string = filePath.toString();
UploadAsyncTask task = new UploadAsyncTask(this);
task.execute(string);
}
}
}
public class UploadAsyncTask extends AsyncTask<String, Integer, Integer> {
ProgressDialog dialog;
Context context;
public UploadAsyncTask(Context context){
this.context = context;
}
@Override
protected Integer doInBackground(String... params) {
try {
String fileName = params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http:/……"); //アップするアドレス
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
MultipartEntity multipartEntity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(fileName);
FileBody fileBody = new FileBody(file, "image/jpeg");
multipartEntity.addPart("f1", fileBody);
httpPost.setEntity(multipartEntity);
httpClient.execute(httpPost, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
@Override
protected void onPostExecute(Integer result) {
if(dialog != null){
dialog.dismiss();
}
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setTitle("Please wait");
dialog.setMessage("Uploading...");
dialog.show();
}
}
--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com にメールを送信します。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-group-japan からこのグループにアクセスしてください。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。