从相机单击后,我试图将图像上传到服务器,但是服务器返回
在服务器上单击图像并上传后,($ _ 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
}
}