public static KrollDict createDictForImage(TiBlob imageData, String mimeType) {
    KrollDict d = new KrollDict();
    d.putCodeAndMessage(NO_ERROR, null);

    int width = -1;
    int height = -1;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;

    // We only need the ContentResolver so it doesn't matter if the root or current activity is used
    // for
    // accessing it
    BitmapFactory.decodeStream(imageData.getInputStream(), null, opts);

    width = opts.outWidth;
    height = opts.outHeight;

    d.put("x", 0);
    d.put("y", 0);
    d.put("width", width);
    d.put("height", height);

    KrollDict cropRect = new KrollDict();
    cropRect.put("x", 0);
    cropRect.put("y", 0);
    cropRect.put("width", width);
    cropRect.put("height", height);
    d.put("cropRect", cropRect);

    d.put("mediaType", MEDIA_TYPE_PHOTO);
    d.put("media", imageData);

    return d;
  }
示例#2
0
  private Object eventToHashMap(SensorEvent event, long timestamp) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    KrollDict heading = new KrollDict();
    heading.put(TiC.EVENT_PROPERTY_TYPE, TiC.EVENT_HEADING);
    heading.put(TiC.PROPERTY_TIMESTAMP, timestamp);
    heading.put(TiC.PROPERTY_X, x);
    heading.put(TiC.PROPERTY_Y, y);
    heading.put(TiC.PROPERTY_Z, z);
    heading.put(TiC.PROPERTY_MAGNETIC_HEADING, x);
    heading.put(TiC.PROPERTY_ACCURACY, event.accuracy);

    if (Log.isDebugModeEnabled()) {
      switch (event.accuracy) {
        case SensorManager.SENSOR_STATUS_UNRELIABLE:
          Log.i(TAG, "Compass accuracy unreliable");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
          Log.i(TAG, "Compass accuracy low");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
          Log.i(TAG, "Compass accuracy medium");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
          Log.i(TAG, "Compass accuracy high");
          break;
        default:
          Log.w(TAG, "Unknown compass accuracy value: " + event.accuracy);
      }
    }

    updateDeclination();
    if (geomagneticField != null) {
      float trueHeading = (x + geomagneticField.getDeclination() + 360) % 360;

      heading.put(TiC.PROPERTY_TRUE_HEADING, trueHeading);
    }

    KrollDict data = new KrollDict();
    data.putCodeAndMessage(TiC.ERROR_CODE_NO_ERROR, null);
    data.put(TiC.PROPERTY_HEADING, heading);
    return data;
  }
    @Override
    public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
      if (resultCode == Activity.RESULT_CANCELED) {
        if (imageFile != null) {
          imageFile.delete();
        }
        if (cancelCallback != null) {
          KrollDict response = new KrollDict();
          response.putCodeAndMessage(NO_ERROR, null);
          cancelCallback.callAsync(getKrollObject(), response);
        }

      } else {
        // If data is null, the image is not automatically saved to the gallery, so we need to
        // process it with
        // the one we created
        if (data == null) {
          processImage(activity);

        } else {
          // Get the content information about the saved image
          String[] projection = {
            Images.Media.TITLE,
            Images.Media.DISPLAY_NAME,
            Images.Media.MIME_TYPE,
            Images.ImageColumns.BUCKET_ID,
            Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaColumns.DATA,
            Images.ImageColumns.DATE_TAKEN
          };

          String title = null;
          String displayName = null;
          String mimeType = null;
          String bucketId = null;
          String bucketDisplayName = null;
          String dataPath = null;
          String dateTaken = null;

          Cursor c = null;
          boolean isDataValid = true;
          boolean isQueriedPhotoValid = true;
          Uri uriData = data.getData();
          if (uriData != null) {
            c = activity.getContentResolver().query(uriData, projection, null, null, null);
          }
          if (c == null) {
            c =
                activity
                    .getContentResolver()
                    .query(
                        Images.Media.EXTERNAL_CONTENT_URI,
                        projection,
                        null,
                        null,
                        Images.ImageColumns.DATE_TAKEN);
            isDataValid = false;
          }
          if (c != null) {
            try {
              boolean isCursorValid = false;
              if (uriData != null && isDataValid) {
                isCursorValid = c.moveToNext();
              } else {
                isCursorValid = c.moveToLast();
              }
              if (isCursorValid) {
                title = c.getString(0);
                displayName = c.getString(1);
                mimeType = c.getString(2);
                bucketId = c.getString(3);
                bucketDisplayName = c.getString(4);
                dataPath = c.getString(5);
                dateTaken = c.getString(6);

                if (!isDataValid && dateTaken.equals(dateTaken_lastImageInExternalContentURI)) {
                  isQueriedPhotoValid = false;
                }

                Log.d(
                    TAG,
                    "Image { title: "
                        + title
                        + " displayName: "
                        + displayName
                        + " mimeType: "
                        + mimeType
                        + " bucketId: "
                        + bucketId
                        + " bucketDisplayName: "
                        + bucketDisplayName
                        + " path: "
                        + dataPath
                        + " }",
                    Log.DEBUG_MODE);
              }
            } finally {
              c.close();
              c = null;
            }
          } else {
            // If we can't get query the image, process it from the imageFile
            processImage(activity);
            return;
          }
          if (!isQueriedPhotoValid) {
            // If the queried image is not the one we just captured, process it from the imageFile
            processImage(activity);
            return;
          }

          String localImageUrl = dataPath;
          URL url;
          try {
            if (!saveToPhotoGallery) {
              // We need to move the image from dataPath to the temp file which will be deleted
              // when the app exits.
              url = new URL(imageUrl);
              moveImage(dataPath, url.getPath());

              // Delete the saved the image entry from the gallery DB.
              if (uriData != null && isDataValid) {
                activity.getContentResolver().delete(uriData, null, null);
              } else {
                activity
                    .getContentResolver()
                    .delete(
                        Images.Media.EXTERNAL_CONTENT_URI,
                        "datetaken = ?",
                        new String[] {dateTaken});
              }

              localImageUrl = imageUrl; // make sure it's a good URL before setting it to pass back.
            } else if (imageUrl != null) {
              // Delete the temp file since we want to use the one from the photo gallery
              url = new URL(imageUrl);
              File source = new File(url.getPath());
              source.delete();
            }
          } catch (MalformedURLException e) {
            Log.e(TAG, "Invalid URL not moving image: " + e.getMessage());
          }
          invokeSuccessCallback(activity, localImageUrl);
        }
      }
    }