Ejemplo n.º 1
0
 private void setupDimension() {
   ParcelFileDescriptor input = null;
   try {
     input = mContentResolver.openFileDescriptor(mUri, "r");
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapManager.instance().decodeFileDescriptor(input.getFileDescriptor(), options);
     mWidth = options.outWidth;
     mHeight = options.outHeight;
   } catch (FileNotFoundException ex) {
     mWidth = 0;
     mHeight = 0;
   } finally {
     Util.closeSilently(input);
   }
 }
  private void saveOutput(Bitmap croppedImage) {
    if (mSaveUri != null) {
      OutputStream outputStream = null;
      try {
        outputStream = mContentResolver.openOutputStream(mSaveUri);
        if (outputStream != null) {
          croppedImage.compress(mOutputFormat, mOutputQuality, outputStream);
        }
      } catch (IOException ex) {
        // TODO: report error to caller
        Log.e(TAG, "Cannot open file: " + mSaveUri, ex);
      } finally {
        Util.closeSilently(outputStream);
      }
      Bundle extras = new Bundle();
      setResult(RESULT_OK, new Intent(mSaveUri.toString()).putExtras(extras));
    } else if (mSetWallpaper) {
      try {
        WallpaperManager.getInstance(this).setBitmap(croppedImage);
        setResult(RESULT_OK);
      } catch (IOException e) {
        Log.e(TAG, "Failed to set wallpaper.", e);
        setResult(RESULT_CANCELED);
      }
    } else {
      Bundle extras = new Bundle();
      extras.putString("rect", mCrop.getCropRect().toString());

      File oldPath = new File(mImage.getDataPath());
      File directory = new File(oldPath.getParent());

      int x = 0;
      String fileName = oldPath.getName();
      fileName = fileName.substring(0, fileName.lastIndexOf("."));

      // Try file-1.jpg, file-2.jpg, ... until we find a filename which
      // does not exist yet.
      while (true) {
        x += 1;
        String candidate = directory.toString() + "/" + fileName + "-" + x + ".jpg";
        boolean exists = (new File(candidate)).exists();
        if (!exists) {
          break;
        }
      }

      try {
        int[] degree = new int[1];
        Uri newUri =
            ImageManager.addImage(
                mContentResolver,
                mImage.getTitle(),
                mImage.getDateTaken(),
                null, // TODO this null is going to cause us to lose
                // the location (gps).
                directory.toString(),
                fileName + "-" + x + ".jpg",
                croppedImage,
                null,
                degree);

        setResult(RESULT_OK, new Intent().setAction(newUri.toString()).putExtras(extras));
      } catch (Exception ex) {
        // basically ignore this or put up
        // some ui saying we failed
        Log.e(TAG, "store image fail, continue anyway", ex);
      }
    }

    final Bitmap b = croppedImage;
    mHandler.post(
        new Runnable() {
          public void run() {
            mImageView.clear();
            b.recycle();
          }
        });

    finish();
  }