1

我想在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");

但它没有将该字符串转换为图像请告诉我解决方案...

4

1 回答 1

0

如果您只需要通过 ftp 从 android 将文件上传到 Web 服务器,这将解决问题(从 froyo 开始工作 - 但您必须导入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();

注意:如您所见,您可以使用 2 个替代构造函数通过存储默认位置来自动化您的 ftp。它们可以被移除而不会造成任何伤害。

于 2013-01-18T16:40:23.617 回答