@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ButterKnife.bind(this); toux.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(intent,2); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); Uri uri = data.getData(); try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); } catch (IOException e) { e.printStackTrace(); } String path = Environment.getExternalStorageDirectory() + "/iooo"; File file = new File(path); //判斷文件是否存在 if (!file.exists()){ file.mkdir(); } filel = new File(file,"aaa.png"); try { //用流寫入 BufferedOutputStream bu = new BufferedOutputStream(new FileOutputStream(filel)); //壓縮 bitmap.compress(Bitmap.CompressFormat.JPEG,100,bu); //刷新 bu.flush(); //關閉流 bu.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } sc(filel); } private void sc(File filel) { RequestBody body = RequestBody.create(MediaType.parse("appliction/otcet-stream"), filel); MultipartBody.Part part = MultipartBody.Part.createFormData("image", filel.getName(), body); RetrofitNet.getInstance().getCreate(Api.ULL, GetService.class) .getInter(4436, "15565254601104436", part) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Bean>() { @Override public void accept(Bean bean) throws Exception { String headPath = bean.getHeadPath(); Log.e("iiii", "ffgffffff" + headPath); toux.setImageURI(Uri.parse(headPath)); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { Log.e("tag", "ee " + throwable.getMessage()); } }); } }
imag上传
最新推荐文章于 2022-08-02 17:02:35 发布