我想在android中开发一个应用程序,我需要捕获图像并将该图像转换为字符串并将该字符串写入txt文件并将其发送到服务器,服务器读取该文件并再次将该字符串转换为图像…
现在我已经完成了图像的一部分,并将该图像转换为字符串并将该字符串写入TXT文件。
但是当我尝试读取该文件并将该字符串转换为图像时,它不起作用…
将图像转换为字符串的代码为
File imageFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
imgstr = Base64.encodeToString(image, 0);
将其写入文件的代码为
File file = new File("new.txt");
FileWriter w = new FileWriter("/sdcard/new/new.txt");
BufferedWriter out = new BufferedWriter(w);
out.write(data);
out.flush();
out.close();
读取文件并将字符串转换为图像的代码是
FileInputStream fstream = new FileInputStream("/sdcard/new/new.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
ArrayList list=new ArrayList();
while ((strLine = br.readLine()) != null)
{
list.add(strLine);
}
Iterator itr;
for (itr=list.iterator(); itr.hasNext(); )
{
String str=itr.next().toString();
StringBuffer sb=new StringBuffer(str);
int length=sb.length();
String imageDataString = sb.substring(0, length);
byte[] decodedString = Base64.decode(imageDataString, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
FileOutputStream imageOutFile = new FileOutputStream("/sdcard/new/android.jpg");
imageOutFile.write(decodedString);
imageOutFile.close();
System.out.println("File converted");
但它没有将字符串转换成图像
如果你所需要的是通过ftp从android上传文件到web服务器,这将做的技巧(从froyo - up工作-但你必须导入apache commons并在你的"src"文件夹中安装一个副本):
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import android.util.Log;
public class FtpFileUp implements Runnable {
private final String TAG = "FTPfile";
boolean flagFTPOK = false;
String fileName, fileDirSubLocalName, fileDirSubRemoteName;
String fileDirName = NavigationActivity.fileDirName;
FtpFileUp (String fileNameIn, String fileDirSubLocalNameIn, String fileDirSubRemoteNameIn) {
fileName = (String) fileNameIn;
fileDirSubLocalName = (String) fileDirSubLocalNameIn;
fileDirSubRemoteName = (String) fileDirSubRemoteNameIn;
}
FtpFileUp (String fileNameIn) {
String fileDirSubNameIn = "";
fileName = (String) fileNameIn;
fileDirSubLocalName = (String) fileDirSubNameIn;
fileDirSubRemoteName = (String) fileDirSubNameIn;
}
FtpFileUp (String fileNameIn, String fileDirSubNameIn) {
fileName = (String) fileNameIn;
fileDirSubLocalName = (String) fileDirSubNameIn;
fileDirSubRemoteName = (String) fileDirSubNameIn;
}
@Override
public void run() {
String ftpConnectString = "ftp.yourdomain.com";
if (fileDirSubRemoteName != "") fileDirSubRemoteName += "/";
if (fileDirSubLocalName != "") fileDirSubLocalName += "/";
FTPClient ftpCli = new FTPClient();
try {
FileInputStream fis = new FileInputStream(fileDirName+"/"+fileDirSubLocalName+fileName);
ftpCli.connect(ftpConnectString);
ftpCli.login("user", "password");
Log.i(TAG, "ok ftp "+ftpCli.getDataConnectionMode());
ftpCli.storeFile("/"+fileDirSubRemoteName+fileName, fis);
fis.close();
flagFTPOK = true;
} catch (Exception except) {
Log.i(TAG, "ftp up FAIL "+except);
}
}
}
,您可以使用以下代码(由TRY CATCH封装)调用
Thread ftpThread01 = new Thread(new FtpFileUp("fileName", "", "/www/android/imgUpload"));
ftpThread01.start();
注意:正如你所看到的,有两个可选的构造函数,你可以使用它们来自动存储默认位置。它们可以被移除而不会造成任何伤害。