private Resource makeResource(Bitmap image) {
   Resource imageResource = new Resource();
   Data resourceData = new Data();
   imageResource.setData(resourceData);
   try {
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     // Write bitmap to file using PNG and 85% quality
     image.compress(Bitmap.CompressFormat.PNG, 85, stream);
     byte[] byteArray = stream.toByteArray();
     stream.close();
     resourceData.setBody(byteArray);
     resourceData.setSize(byteArray.length);
     resourceData.setBodyHash(Arrays.copyOfRange(md5(byteArray), 0, Constants.EDAM_HASH_LEN));
     imageResource.setMime(Constants.EDAM_MIME_TYPE_PNG);
     imageResource.setHeight((short) image.getHeight());
     imageResource.setWidth((short) image.getWidth());
     image.recycle();
   } catch (IOException ioe) {
     Log.e(
         FingerPaintView.class.getName(), "makeResource [IOException]: " + ioe.getMessage(), ioe);
     imageResource = null;
   }
   return imageResource;
 }
예제 #2
0
  // Convert the binary data to a resource
  private Resource createResource(byte[] fileData, String address) {
    logger.log(logger.EXTREME, "Inside create resource");
    // These two lines are added to handle odd characters in the name like #.  Without it
    // toLocalFile() chokes and returns the wrong name.
    MessageDigest md;
    try {
      logger.log(logger.EXTREME, "Generating MD5");
      md = MessageDigest.getInstance("MD5");
      md.update(fileData);
      byte[] hash = md.digest();

      Resource r = new Resource();
      r.setGuid(QUuid.createUuid().toString().replace("}", "").replace("{", ""));
      r.setNoteGuid(noteGuid);
      r.setMime("image/" + address.substring(address.lastIndexOf(".") + 1));
      r.setActive(true);
      r.setUpdateSequenceNum(0);
      r.setWidth((short) 0);
      r.setHeight((short) 0);
      r.setDuration((short) 0);

      Data d = new Data();
      d.setBody(fileData);
      d.setBodyIsSet(true);
      d.setBodyHash(hash);
      d.setBodyHashIsSet(true);
      r.setData(d);
      d.setSize(fileData.length);

      int fileNamePos = address.lastIndexOf(File.separator);
      if (fileNamePos == -1) fileNamePos = address.lastIndexOf("/");
      String fileName = address.substring(fileNamePos + 1);
      ResourceAttributes a = new ResourceAttributes();
      a.setAltitude(0);
      a.setAltitudeIsSet(false);
      a.setLongitude(0);
      a.setLongitudeIsSet(false);
      a.setLatitude(0);
      a.setLatitudeIsSet(false);
      a.setCameraMake("");
      a.setCameraMakeIsSet(false);
      a.setCameraModel("");
      a.setCameraModelIsSet(false);
      a.setAttachment(false);
      a.setAttachmentIsSet(true);
      a.setClientWillIndex(false);
      a.setClientWillIndexIsSet(true);
      a.setRecoType("");
      a.setRecoTypeIsSet(false);
      a.setSourceURL(fileName);
      a.setSourceURLIsSet(true);
      a.setTimestamp(0);
      a.setTimestampIsSet(false);
      a.setFileName(fileName);
      a.setFileNameIsSet(true);
      r.setAttributes(a);

      // conn.getNoteTable().noteResourceTable.saveNoteResource(r, true);
      logger.log(logger.EXTREME, "Resource created");

      // Now write it out so someone else can look at it

      return r;
    } catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    }
    return null;
  }