无法将相机单击的图像上传到服务器

从相机单击后,我试图将图像上传到服务器,但是服务器返回

在服务器上单击图像并上传后,

($ _ File)JSON响应

BiFunction
从库中选择图像并上传后,

($ _ File)JSON响应

{
   "data":78,"status":true,"files":
   {
      "photo": 
      {
         "name":"IMG_20191108_115642_5386652903586463966.jpg","type":"","tmp_name":"","error":1,"size":0
      }
    }
  }
    {
     "data":79,"files":
         {
           "photo": 
              {
               "name":"Screenshot_20191108_081937_com.instagram.android.jpg","type":"*\/*","tmp_name":"C:\\xampp\\tmp\\php50A6.tmp","error":0,"size":518164
              }
         }
   }


相机意图

     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pharmacy)
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) !=
            PackageManager.PERMISSION_GRANTED
        ) {
            activityCompat.requestPermissions(this,permission,REQUEST_PERMISSION)
        }
    next.setOnClicklistener {

            if (prescriptionid == "") {
                Toast.makeText(
                    applicationContext,"Select/Upload Prescription First",Toast.LENGTH_LONG
                ).show()
            } else {
                intent = Intent(applicationContext,SelectAddressactivity::class.java)
                imageFilePath = ""
                imageView.setImageResource(R.drawable.ic_image)
                imagedisplay.visibility = View.GONE
                startactivity(intent)
            }
        }

        if (imageFilePath == "") {
            imagedisplay.visibility = View.GONE
        } else {
            imagedisplay.visibility = View.GONE
        }
     }

GalleryIntent

private fun openCameraIntent() {
        val pictureIntent = Intent(
            MediaStore.actION_IMAGE_CAPTURE)
        if (pictureIntent.resolveactivity(getPackageManager()) != null)
        {
            try
            {
                photoFile = createImageFile()
            }
            catch (ex:IOException) {}// Error occurred while creating the File
            if (photoFile != null)
            {
                val photoURI = FileProvider.geturiForFile(this,packageName+".provider",photoFile)
                pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI)
                startactivityForResult(pictureIntent,CAMERA_REQUEST_CODE)
            }
        }
    }
private fun pickFromGallery() {
        //Create an Intent with action as actION_PICK
        val intent = Intent(Intent.actION_PICK)
        // Sets the type as image/*. This ensures only components of type image are selected
        intent.type = "image/*"
        //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
        val mimeTypes = arrayOf("image/jpeg","image/png")
        intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes)
        // launching the Intent
        startactivityForResult(intent,GALLERY_REQUEST_CODE)
    }

创建图像文件的代码

 override fun onactivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
        super.onactivityResult(requestCode,resultCode,data)
        imagedisplay.visibility = View.VISIBLE
        when (requestCode) {
            CAMERA_REQUEST_CODE -> {
                if (resultCode == activity.RESULT_OK) {
                    correct.visibility = View.VISIBLE
                    imageView.setImageBitmap(setScaledBitmap())
                }
            }
           GALLERY_REQUEST_CODE -> {
                //data.getData returns the content URI for the selected Image
                if (resultCode == activity.RESULT_OK) {
                    correct.visibility = View.VISIBLE
                    var selectedImage = data!!.data as Uri
                    var filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
                    // Get the cursor
                    var cursor = getcontentResolver().query(
                        selectedImage,filePathColumn,null,null
                    );
                    // Move to first row
                    cursor!!.moveToFirst();
                    //Get the column index of MediaStore.Images.Media.DATA
                    var columnIndex = cursor.getcolumnIndex(filePathColumn[0])
                    //Gets the String value in the column
                    var imgDecodableString = cursor.getString(columnIndex)
                    cursor.close()
                    // Set the Image in ImageView after decoding the String
                    Log.i("filepath",imgDecodableString)
                    imageFilePath = imgDecodableString
                    imageView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString))

                }
            }
            else -> {
                Toast.makeText(this,"Unrecognized request code",Toast.LENGTH_SHORT).show()
            }
        }
    }

将图片上传到服务器的代码


    @Throws(IOException::class)
    private fun createImageFile(): File {
        // Create an image file name
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES) as File
        return File.createTempFile(
            "IMG_${timeStamp}_",/* prefix */
            ".jpg",/* suffix */
            storageDir /* directory */
        ).apply {
            // Save a file: path for use with actION_VIEW intents
            imageFilePath = absolutePath
        }
    }
bcp123 回答:无法将相机单击的图像上传到服务器

允许上传的最大文件大小为2Mb。正如您在comment中提到的,要上传的照片大小为4Mb。

如果您要上传大于设置的大小的文件,只需打开php.ini文件以增加大小。

在任何您喜欢的文本编辑器中打开C:/xampp/php/php.ini文件。搜索upload_max_filesize并更改其大小。例如50Mb

upload_max_filesize = 50M    

PHP还将接受的POST数据的最大大小为8Mb。如果要扩展它,请搜索post_max_size并增加其大小。

最后,不要忘记重启Apache服务器。

本文链接:https://www.f2er.com/3139842.html

大家都在问