【发布时间】:2018-07-25 21:41:35
【问题描述】:
我想从 android 设备中选择一个文件,将该文件转换为字符串格式,将该字符串转换为 BASE64 编码,然后通过 API 将该编码字符串传递给服务器。
我调试了我的项目,它上升到这一行:
FileInputStream fis = new FileInputStream(new File(path));
它不会走得更远。
谁能告诉我哪里做错了?
提前致谢。
这是我的代码:
package com.example.deepb.testingencodingapp;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Base64;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText fatchValue;
TextView displayValue,Fpath;
Button GetValue,SelectFile;
String ret,uriString,finalString;
File myFile;
String line = null;
private static final int PICKFILE_RESULT_CODE = 1;
public static final int RESULT_OK = -1;
Uri uri;
byte[] fileBytes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fatchValue = findViewById(R.id.enterText);
displayValue = findViewById(R.id.displayText);
Fpath = findViewById(R.id.pathText);
GetValue = findViewById(R.id.btn_go);
SelectFile = findViewById(R.id.uploadFile);
GetValue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String GotValue = fatchValue.getText().toString();
byte[] encodeValue = Base64.encode(finalString.getBytes(), Base64.DEFAULT);
String ansValue = encodeValue.toString();
//displayValue.setText(ansValue);
displayValue.setText(finalString);
Log.d(finalString,"this is the String " + finalString);
}
});
SelectFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getFile = new Intent(Intent.ACTION_GET_CONTENT);
getFile.setType("*/*");
startActivityForResult(getFile,PICKFILE_RESULT_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
uri = data.getData();
uriString = uri.toString();
myFile = new File(uriString);
ret = myFile.getAbsolutePath();
Fpath.setText(ret);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String path = ret;
FileInputStream fis = new FileInputStream(new File(path));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
fileBytes = baos.toByteArray();
//Log.i("ByteArray" + path + ">><<" + ret, "----" + fileBytes.toString());
finalString = fileBytes.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
【问题讨论】:
-
你遇到了什么错误?
-
不是一个错误..它完美地执行到这一行
FileInputStream fis = new FileInputStream(new File(path));然后我得到“fileBytes”= null。所以“finalString = null”..这就是我在 Logcat 中得到的 .. /com.example.deepb.testingencodingapp E/Activity:onResume-Exception-e:尝试调用接口方法'void android.app.applock.ILavaAppLockService startLockMonitor(java.lang.String, java.lang.String)' 在空对象引用 /com.example.deepb.testingencodingapp E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE 跨度不能有零长度 -
嘿@GinoMempin ..可能的问题是
FileInputStream..这就是我的想法..你可以为我提供任何解决方案吗?我做错了什么或哪里出错了? -
嗯。当你说“它会上升到这条线”和“它不会更进一步。”时,我最初的理解是它停止运行或在
new FileInputStream(new File(path));处引发异常。所以 实际 问题在于读取文件。如果如您所说的“fileBytes = null”,那么您应该在finalString = fileBytes.toString();获得NullPointerException。它真的是空的还是空的/零?顺便说一句,您共享的 logcat 错误与问题无关(这是使用 EditText 时的常见错误)。 -
由于您真正需要的文件内容为
String,因此请尝试使用BufferedReader将其读取为String而不是字节。类似stackoverflow.com/questions/12910503/read-file-as-string/….
标签: android string base64 fileinputstream