提问者:小点点

将java转换为kotlin[副本]


我不能在kotlin中找到任何示例,但在java中找到一个,但想将其转换为kotlin,以便它可以与我的应用程序一起工作。

public void clickFoto(View view) {
        takePic();
    }

//取Pic()

 private void takePic() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        *File file = new File(Environment.getExternalStorageDirectory(), "/your_name_folder/a" + "/photo_" + timeStamp + ".png");*
        imageUri = Uri.fromFile(file);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, PICTURE_RESULT);
    }

//onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case PICTURE_RESULT:
                if (requestCode == PICTURE_RESULT)
                    if (resultCode == Activity.RESULT_OK) {
                        try {
                            Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                    getContentResolver(), imageUri);
                            imgFoto.setImageBitmap(thumbnail);
                            imageurl = getRealPathFromURI(imageUri);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
        }
    }

//getRealPathFromUri

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

共1个答案

匿名用户

您可以在Android Studio中找到将Java转换为静态编程语言文件菜单(Ctrl Shift Alt K)。

无论如何,使用转换,您的代码将是这样的。

fun clickFoto(view:View) {
  takePic()
}

private fun takePic() {
  val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
  val file = File(Environment.getExternalStorageDirectory(), "/your_name_folder/a" + "/photo_" + timeStamp + ".png")
  imageUri = Uri.fromFile(file)
  val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
  startActivityForResult(intent, PICTURE_RESULT)
}


fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) {
  when (requestCode) {
    PICTURE_RESULT -> if (requestCode == PICTURE_RESULT)
    if (resultCode == Activity.RESULT_OK)
    {
      try
      {
        val thumbnail = MediaStore.Images.Media.getBitmap(
          getContentResolver(), imageUri)
        imgFoto.setImageBitmap(thumbnail)
        imageurl = getRealPathFromURI(imageUri)
      }
      catch (e:Exception) {
        e.printStackTrace()
      }
    }
  }
}

fun getRealPathFromURI(contentUri:Uri):String {
  val proj = arrayOf<String>(MediaStore.Images.Media.DATA)
  val cursor = managedQuery(contentUri, proj, null, null, null)
  val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
  cursor.moveToFirst()
  return cursor.getString(column_index)
}