Wie konvertiert base64 zu pdf?

Gegeben, die eine base64-Eingang, wie würde ich es konvertieren in eine PDF-Datei in Android?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        //Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            //Files is from Guava library
            Files.toByteArray(file);
            //Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            //For testing purposes, wrote the encoded string to file
            writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());

        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

Konnte ich http://www.webutils.pl/index.php?idx=base64 zum test die PDF-Datei wurde korrekt kodiert. Aber ich möchte in der Lage sein zum decodieren des base64 zurück zu PDF selbst. Wie soll ich das tun?

Bearbeiten 1

Versucht den code unten vorgeschlagen von Saneesh CS

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        //Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            //Files is from Guava library
            Files.toByteArray(file);
            //Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            //Tried with the below line, but same result.
            byte[] temp1 = Base64.encode(Files.toByteArray(file), 0);
            //For testing purposes, wrote the encoded string to file
//         writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());
            final File dwldsPath = new File(Environment.getExternalStorageDirectory(), "test7.pdf");
            byte[] pdfAsBytes = Base64.decode(temp, 0);
            FileOutputStream os;
            os = new FileOutputStream(dwldsPath, false);
            os.write(pdfAsBytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

Obige code funktioniert.

  • Der code im Edit-1 ist die Codierung, nicht Dekodieren.
  • Danke, gut zu fangen. Es funktioniert jetzt.
InformationsquelleAutor Noman Arain | 2013-11-13
Schreibe einen Kommentar