Anasbzr
Anasbzr

Reputation: 59

failed to decode image from parse

I am trying to decode the image that i am getting from parse while i send it to another functions for decryption, however i can't seem to get the file to decrypt correctly as on the decode line i get that the string was null therefore nothing to decode, How can i retrieve the parse file and decrypt then send it to decode.

My Parse function

case R.id.decryptButton:


    Intent i = getIntent();
    image = i.getStringExtra("image");
    progressDialog = ProgressDialog.show(SingleFileView.this, "",
    "Downloading Image...", true);

    // Locate the class table named "ImageUpload" in Parse.com
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                                                           "NewFiles");

    // Locate the objectId from the class
         query.getInBackground(objectId,
         new GetCallback<ParseObject>() {

         public void done(ParseObject object,
            ParseException e) {
            // TODO Auto-generated method stub

            // Locate the column named "ImageName" and set
            // the string

           final ParseFile fileObject = (ParseFile) object
               .get("ImageFile");
               fileObject
               .getDataInBackground(new GetDataCallback() {
                  public void done(byte[] data,
                     ParseException e) {
                        if (e == null) {
                          Log.d("test",
                            "We've got data in data.");
                            // Decode the Byte[] into
                            // Bitmap
                            FileInputStream fis = null;
                            saveFile(data, "temp.jpeg");

                            try {
                             File xx = new File(Environment.getExternalStorageDirectory()+"/temp.jpeg");
                             fis = new FileInputStream(String.valueOf(data));
                             System.out.print(fileObject.getUrl());
                             System.out.print(fis);

                             } catch (FileNotFoundException e1) {
                             e1.printStackTrace();
                             }
                             byte[] dmg = decrypt(key, fis);
                             Bitmap bmp = BitmapFactory
                            .decodeByteArray(
                            dmg, 0,
                            dmg.length);

                          // Get the ImageView from
                          // main.xml
                          ImageView image = (ImageView) findViewById(R.id.image);

                          // Set the Bitmap into the
                          // ImageView
                          image.setImageBitmap(bmp);

                         // Close progress dialog
                         progressDialog.dismiss();

                         } else {
                         Log.d("test",
                         "There was a problem downloading the data.");
                         }
                      }
                   });
         }
});

My Decrypt and Save File functions

private byte[] decrypt(byte[] skey, InputStream fis){

    SecretKeySpec skeySpec = new SecretKeySpec(skey, "AES");
    Cipher cipher;
    byte[] decryptedData=null;
    CipherInputStream cis=null;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
        // Create CipherInputStream to read and decrypt the image data
        cis = new CipherInputStream(fis, cipher);
        // Write encrypted image data to ByteArrayOutputStream
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[2048];
        while ((cis.read(data)) != -1) {
            buffer.write(data);
        }
        buffer.flush();
        decryptedData=buffer.toByteArray();

    }catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try {
            fis.close();
            cis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return decryptedData;
}
public void saveFile(byte[] data, String outFileName){
    FileOutputStream fos=null;
    try {
        fos=new FileOutputStream(Environment.getExternalStorageDirectory()+File.separator+outFileName);
        fos.write(data);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I don't seem to locate the issue with the file as i am unsure whether the problem is with the decryption method or the file i am getting from parse is done incorrectly.

Upvotes: 0

Views: 94

Answers (1)

zaph
zaph

Reputation: 112857

The error is: "the string was null therefore nothing to decode" which says there is no input, that is what to look at. IOW, debug, breakpoints, single steeping in the debugger, print variables.

Upvotes: 1

Related Questions