Exemplo n.º 1
0
  public static MessageData addAttachmentFromPath(MessageData draft, String path)
      throws FileNotFoundException {

    if (TextUtils.isEmpty(path)) {
      throw new FileNotFoundException();
    }

    File phy = new File(path); // physical
    File vir = new File(path); // virtual, change if needed

    try {
      FileInputStream is = new FileInputStream(phy.getAbsolutePath());
      try {
        byte[] outFileData = new byte[is.available()];
        is.read(outFileData);
        draft.setFileData(outFileData);
        draft.setFileSize(outFileData.length);

        String type = URLConnection.guessContentTypeFromStream(is);
        if (type != null) draft.setFileType(type);
        else {
          String extension = SSUtil.getFileExtensionOnly(vir.getName());
          type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
          if (type != null) {
            draft.setFileType(type);
          } else {
            draft.setFileType(SafeSlingerConfig.MIMETYPE_OPEN_ATTACH_DEF);
          }
        }
      } finally {
        is.close();
      }
    } catch (OutOfMemoryError e) {
      return draft;
    } catch (IOException e) {
      return draft;
    }
    draft.setFileName(vir.getName());
    draft.setFileDir(phy.getPath());
    draft.setMsgHash(null);

    return draft;
  }
Exemplo n.º 2
0
  public static MessageData addAttachmentFromUri(
      Context ctx, MessageData draft, Uri uri, String contentType) throws IOException {
    String name = null;
    try {
      Cursor c =
          ctx.getContentResolver()
              .query(uri, new String[] {MediaColumns.DISPLAY_NAME}, null, null, null);
      if (c != null) {
        try {
          if (c.moveToFirst()) {
            name = c.getString(c.getColumnIndex(MediaColumns.DISPLAY_NAME));
          }
        } finally {
          c.close();
        }
      }
    } catch (IllegalArgumentException e) {
      // column may not exist
    }

    long size = -1;
    try {
      Cursor c =
          ctx.getContentResolver().query(uri, new String[] {MediaColumns.SIZE}, null, null, null);
      if (c != null) {
        try {
          if (c.moveToFirst()) {
            size = c.getInt(c.getColumnIndex(MediaColumns.SIZE));
          }
        } finally {
          c.close();
        }
      }
    } catch (IllegalArgumentException e) {
      // column may not exist
    }

    String data = null;
    try {
      Cursor c =
          ctx.getContentResolver().query(uri, new String[] {MediaColumns.DATA}, null, null, null);
      if (c != null) {
        try {
          if (c.moveToFirst()) {
            data = c.getString(c.getColumnIndex(MediaColumns.DATA));
          }
        } finally {
          c.close();
        }
      }
    } catch (IllegalArgumentException e) {
      // column may not exist
    } catch (IllegalStateException e) {
      // column may not exist
    }

    if (name == null) {
      name = uri.getLastPathSegment();
    }

    File f = null;
    if (size <= 0) {
      String uriString = uri.toString();
      if (uriString.startsWith("file://")) {
        f = new File(uriString.substring("file://".length()));
        size = f.length();
      }
    }

    ContentResolver cr = ctx.getContentResolver();
    InputStream is = null;
    // read file bytes
    try {
      is = cr.openInputStream(uri);
    } catch (FileNotFoundException e) {
      if (!TextUtils.isEmpty(data)) {
        is = new FileInputStream(data);
      } else {
        return draft; // unable to load file at all
      }
    }

    if ((contentType != null) && (contentType.indexOf('*') != -1)) {
      contentType = ctx.getContentResolver().getType(uri);
    }

    if (contentType == null) {
      contentType = URLConnection.guessContentTypeFromStream(is);
      if (contentType == null) {
        String extension = SSUtil.getFileExtensionOnly(name);
        contentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (contentType == null) {
          contentType = SafeSlingerConfig.MIMETYPE_OPEN_ATTACH_DEF;
        }
      }
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[4096];
    while (is.read(buf) > -1) {
      baos.write(buf);
    }
    baos.flush();

    final byte[] fileBytes = baos.toByteArray();
    draft.setFileData(fileBytes);
    draft.setFileSize(fileBytes.length);
    draft.setFileType(contentType);
    draft.setFileName(name);
    if (f != null && f.exists()) {
      draft.setFileDir(f.getAbsolutePath());
    } else if (!TextUtils.isEmpty(data)) {
      draft.setFileDir(new File(data).getAbsolutePath());
    }
    return draft;
  }