posts - 296,comments - 1,views - 2995

1.我昨天的成就

完成了图片传输接口

2.遇到什么困难

图片一直传不过去,查阅了很多资料终于能用了

3.今天的任务

调用传输图片接口

4.代码/截屏
private void scImage() {
//1、创建请求体
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)//请求类型
.addFormDataPart("name", "lisi")//参数1

            // 第一个参数传到服务器的字段名,第二个你自己的文件名,第三个MediaType.parse("*/*")数据类型,这个是所有类型的意思,file就是我们之前创建的全局file,里面是创建的图片
            .addFormDataPart("uploadfile", "uploadfile", RequestBody.create(MediaType.parse("*/*"), file))
            .build();
    //2、调用工具类上传图片以及参数
    HttpUtil.uploadFile("http://192.168.24.148:8080/FishClass/upload", requestBody, new Callback() {

        //请求失败回调函数
        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("=============");
            System.out.println("异常::");
            e.printStackTrace();
        }

        //请求成功响应函数
        @Override
        public void onResponse(Call call, Response response) {

            //showResponse(response.body().string());//在主线程中显示提示框
            Log.d("onResponse","success");

        }

    });
}

private void askPermission(){
    ActivityCompat.requestPermissions((Activity) requireContext(),new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    },0);
}
private void choosePic()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(requireContext()).setTitle("请选择图片")
            .setItems(items, (dialog, which) -> {
                if(which==0)
                {
                    openCamera();
                }
                else{
                    openGallery();
                }
            });
    builder.create()
            .show();
}
private void openGallery() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    mResultLauncher.launch(intent);
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void openCamera(){
    String imageName = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date());
    //File outputImage = new File(requireContext().getExternalCacheDir(), imageName+".jpg");
    File outputImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), imageName+".jpg");
    Objects.requireNonNull(outputImage.getParentFile()).mkdirs();
    try
    {
        if(outputImage.exists())
        {
            outputImage.delete();
        }
        boolean a = outputImage.createNewFile();
        Log.e("createNewFile", String.valueOf(a));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    imageUri= FileProvider.getUriForFile(requireContext(), "com.example.teamwork.fileprovider",outputImage);
    path=imageUri.getPath();

// String immmmm1=getRealPathFromUri(imageUri);
// Log.e(immmmm1, immmmm1);
Log.e("存储的拍照路径为:::",path);
Intent intent0=new Intent("android.media.action.IMAGE_CAPTURE");
intent0.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent0,TAKE_PHOTO);

}
posted on   leapss  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2023-04-24 每日打卡-12
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示