我如何从Android内部存储中读取文件并将其作为方法输入

发布于 2025-02-06 08:06:29 字数 3684 浏览 0 评论 0原文

在文本视图中显示文件路径&吐司:

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

    assert data != null;
    textView.setText(data.getDataString());

    Toast.makeText(this.getApplicationContext(), data.getDataString(), Toast.LENGTH_LONG).show();

    super.onActivityResult(requestCode, resultCode, data);
}

我想从手机中选择一个文件,然后将其作为方法输入。 如何在Android Studio中获取文件的路径? 我已经在Eclipse中使用了此代码,并且在手动提供文件路径时可以工作:

public static void Encrypt(String path) throws Exception{



        // file to be encrypted
        FileInputStream inFile = new FileInputStream(path);

        // encrypted file
        FileOutputStream outFile = new FileOutputStream("encryptedfile.des");

        // password to encrypt the file
        String password = "password";

        byte[] salt = new byte[8];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(salt);
        FileOutputStream saltOutFile = new FileOutputStream("salt.enc");
        saltOutFile.write(salt);
        saltOutFile.close();

        SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,
                256);
        SecretKey secretKey = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();

      
        FileOutputStream ivOutFile = new FileOutputStream("iv.enc");
        byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        ivOutFile.write(iv);
        ivOutFile.close();

        //file encryption
        byte[] input = new byte[64];
        int bytesRead;

        while ((bytesRead = inFile.read(input)) != -1) {
            byte[] output = cipher.update(input, 0, bytesRead);
            if (output != null)
                outFile.write(output);
        }

        byte[] output = cipher.doFinal();
        if (output != null)
            outFile.write(output);

        inFile.close();
        outFile.flush();
        outFile.close();


    }

我想将文件路径提供给上面的FileInputStream。我该怎么做?

以下是调用加密方法的MainActivity类,其连接到按钮加密:

public class MainActivity extends AppCompatActivity {


public static TextView textView,message;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    message = findViewById(R.id.textView2);
}

public void BrowseButton(View view) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    assert data != null;
    textView.setText(data.getDataString());
    Toast.makeText(this.getApplicationContext(), data.getDataString(), Toast.LENGTH_LONG).show();
    super.onActivityResult(requestCode, resultCode, data);
}

public void Encrypt(View view) throws Exception {
    Encryption.Encrypt();
}

public void Decrypt(View view) throws Exception{
    Decryption.Decrypt();
}

}

我对加密调用进行了以下更改:

 public void Encrypt(View view) throws Exception {
    String path= String.valueOf(textView);
    Encryption.Encrypt(path);
}

TextView显示了用户浏览的文件路径。 ERORRS: java.lang.illegalstateException:无法执行Android的方法:OnClick

Displaying the File path in the textview & Toast:

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

    assert data != null;
    textView.setText(data.getDataString());

    Toast.makeText(this.getApplicationContext(), data.getDataString(), Toast.LENGTH_LONG).show();

    super.onActivityResult(requestCode, resultCode, data);
}

I want to select a file from my phone and then give it as input to a method.
How to get the path of the file in android studio?
I have used this code in eclipse and it works when I give the file path manually:

public static void Encrypt(String path) throws Exception{



        // file to be encrypted
        FileInputStream inFile = new FileInputStream(path);

        // encrypted file
        FileOutputStream outFile = new FileOutputStream("encryptedfile.des");

        // password to encrypt the file
        String password = "password";

        byte[] salt = new byte[8];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(salt);
        FileOutputStream saltOutFile = new FileOutputStream("salt.enc");
        saltOutFile.write(salt);
        saltOutFile.close();

        SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,
                256);
        SecretKey secretKey = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();

      
        FileOutputStream ivOutFile = new FileOutputStream("iv.enc");
        byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        ivOutFile.write(iv);
        ivOutFile.close();

        //file encryption
        byte[] input = new byte[64];
        int bytesRead;

        while ((bytesRead = inFile.read(input)) != -1) {
            byte[] output = cipher.update(input, 0, bytesRead);
            if (output != null)
                outFile.write(output);
        }

        byte[] output = cipher.doFinal();
        if (output != null)
            outFile.write(output);

        inFile.close();
        outFile.flush();
        outFile.close();


    }

I want to give the path of the file to the fileinputstream above. How can i do that?

The following is the mainactivity class where the encryption method is called, its connected to button encrypt:

public class MainActivity extends AppCompatActivity {


public static TextView textView,message;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    message = findViewById(R.id.textView2);
}

public void BrowseButton(View view) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    assert data != null;
    textView.setText(data.getDataString());
    Toast.makeText(this.getApplicationContext(), data.getDataString(), Toast.LENGTH_LONG).show();
    super.onActivityResult(requestCode, resultCode, data);
}

public void Encrypt(View view) throws Exception {
    Encryption.Encrypt();
}

public void Decrypt(View view) throws Exception{
    Decryption.Decrypt();
}

}

I made the following changes to the Encryption call :

 public void Encrypt(View view) throws Exception {
    String path= String.valueOf(textView);
    Encryption.Encrypt(path);
}

The textView displays the path of the file browsed by the user.
Erorrs:
java.lang.IllegalStateException: Could not execute method for android:onClick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文