当我尝试将图像从我的应用程序上传到桌面时,我不断收到“上传期间出错”消息

时间:2019-11-19 09:19:29

标签: java image file-upload server

基本上,用户应该能够从其画廊/相册中选择一个图像,然后为其命名,然后再将其上传到服务器。我可以选择图像并命名,但是每次尝试上传时,都会收到“上传过程中文件上传错误”通知。

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private Button buttonChoose;
private Button buttonUpload;
private ImageView imageView;
private EditText editText;

private static final int STORAGE_PERMISSION_CODE = 2342;
private static final int PICK_IMAGE_REQUEST = 22;
private Uri filePath;
private Bitmap bitmap;

private static final String UPLOAD_URL = "http://192.168.15.132/C:/Users/Keymas/Desktop/ScanPics";




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    buttonUpload = findViewById(R.id.buttonUpload);
    buttonChoose = findViewById(R.id.buttonChoose);

    imageView = findViewById(R.id.imageView);
    editText = findViewById(R.id.editTextName);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);

    requestStoragePermission();


}

private void requestStoragePermission(){
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
    return;

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == STORAGE_PERMISSION_CODE){
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();

        }else{

            Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show();

        }

    }
}

private void showFileChooser(){
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),filePath);
            imageView.setImageBitmap(bitmap);

        }catch (IOException e){

        }


    }
}

private String getPath(Uri uri){
    Cursor cursor = getContentResolver().query(uri, null,null,null,null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":")+1);
    cursor.close();

    cursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ",
            new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();
    return path;
}

private void uploadImage(){
    String name = editText.getText().toString().trim();
    String path = getPath(filePath);

    try{
        String uploadid = UUID.randomUUID().toString();

        new MultipartUploadRequest(this, uploadid, UPLOAD_URL).addFileToUpload(path, "image").
                addParameter("name", name).setNotificationConfig((new UploadNotificationConfig())).setMaxRetries(2)
        .startUpload();

    }catch (Exception e){

    }

}



@Override
public void onClick(View view) {

    if ( view == buttonUpload){
        uploadImage();
    }
    if(view == buttonChoose){
        showFileChooser();


    }


}

}

0 个答案:

没有答案