1

RSA の公開秘密鍵ペアを生成する暗号化アプリを作成しています。秘密鍵はデバイスに保存する必要があります。通常の Java アプリケーションでのテストでは、キーが生成され、次のファイルに保存されました。

public static void saveToFile(String fileName,BigInteger mod, BigInteger exp) throws IOException
   {
    ObjectOutputStream oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
        try
        {
                oout.writeObject(mod);
                oout.writeObject(exp);
        }

        catch (Exception e)
        {
                throw new IOException("Unexpected error", e);
        }

        finally
        {
                oout.close();
        }
}

キー ファイルはプロジェクト ディレクトリに表示されます。ただし、Android アプリでは、これは発生しません。Androidアプリでファイルを書き込むにはどうすればよいですか?

ありがとう!

4

2 に答える 2

2

キー ファイルはプロジェクト ディレクトリに表示されます。ただし、Android アプリでは、これは発生しません。Androidアプリでファイルを書き込むにはどうすればよいですか?

Android では、アプリケーションがファイルを書き込むことができる主要な場所は 2 つだけです。プライベートな内部ストレージ ディレクトリと外部ストレージ ボリュームです。ファイル名を指定するだけでなく、これらの場所を含むフル パスを指定する必要があります。

//Internal storage location using your filename parameter
File file = new File(context.getFilesDir(), filename);

//External storage location using your filename parameter
File file = new File(Environment.getExternalStorage(), filename);

内部ストレージの違いは、アプリからのみアクセスできます。外部ストレージを USB 経由で接続してマウントすると、PC を含むどこからでも外部ストレージを読み書きできます。

次に、適切なファイルをFileOutputStream既存のコードでラップできます。

于 2013-03-30T00:42:27.287 に答える
0

メソッドとして呼び出す最初のメインクラスを渡します。

Boolean writfile;
writfile =savTextFileInternal(this.getApplicationContext(),"Maa","Ambika");
Toast.makeText(this, "File write:"+writfile, Toast.LENGTH_LONG).show();

次のようなメソッドを作成します。

public boolean savTextFileInternal(Context context,String sFileName, String sBody)
{
    try
    {
        File root = new File(context.getFilesDir(),"myfolder");

        if (!root.exists()) {
            root.mkdirs();
        }

        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        return  true;  
    }
    catch(IOException e)
    {
        e.printStackTrace();
        return false;
    }
}
于 2016-11-24T14:37:08.613 に答える