Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使用してAndroid上のアセットからファイルを読み取る方法は?


この例は、Kotlinを使用してAndroid上のアセットからファイルを読み取る方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、ファイルに移動しますか?新しいプロジェクトを作成し、必要なすべての詳細を入力して新しいプロジェクトを作成します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="100dp"
   android:layout_centerHorizontal="true"
   android:text="Tutorials Point"
   android:textAlignment="center"
   android:textColor="@android:color/holo_green_dark"
   android:textSize="32sp"
   android:textStyle="bold" />
<TextView
   android:id="@+id/textView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:padding="24sp"
   android:textAlignment="center"
   android:textColor="@android:color/holo_purple"
   android:textStyle="bold" />
<Button
   android:id="@+id/btnReadText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerInParent="true"
   android:layout_marginBottom="30dp"
   android:text="Read text from assets"
   android:textStyle="bold" />
</RelativeLayout>

ステップ3 −アプリを右クリック>>新規>>フォルダ>>アセットフォルダ。アセットフォルダを右クリックし、[新規]>>ファイル(myText.txt)とテキストを選択します。

ステップ4 −次のコードをsrc / MainActivity.kt

に追加します
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
import java.io.InputStream
class MainActivity : AppCompatActivity() {
   private lateinit var button: Button
   private lateinit var textView: TextView
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      textView = findViewById(R.id.textView)
      button = findViewById(R.id.btnReadText)
      button.setOnClickListener {
         var string: String? = ""
         try {
            val inputStream: InputStream = assets.open("myText.txt")
            val size: Int = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            string = String(buffer)
         } catch (e: IOException) {
            e.printStackTrace()
         }
         textView.text = string
      }
   }
}

ステップ5 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからKotlinを使用してAndroid上のアセットからファイルを読み取る方法は? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します

Kotlinを使用してAndroid上のアセットからファイルを読み取る方法は?



  1. Androidでstring.xmlから値を読み取る方法は?

    この例は、Androidでstring.xmlから値を読み取る方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / values/strings.xmlに追加します <resources>    <string name="app_name">Sample</string>    <string

  2. Android から PC にファイルを転送する方法

    Android から PC にファイルを転送: 現在、私たちは PC よりも携帯電話を頻繁に使用しています。したがって、ほとんどのファイルが一般的に PC ではなくスマートフォンに存在するのは当然のことです。ここでの唯一の問題は、Android または iPhone には、ユーザーが超えることができないメモリ制限があることです。したがって、すべてのデータを携帯電話よりも空き容量のある PC に保存することは理にかなっています。 しかし、既存のファイルを Android から PC に転送するのも大変な作業です。すべてのファイルとフォルダーを携帯電話から PC に手動で転送することを計画して