Пример #1
0
  public boolean loadFromUri(Uri uri) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {
      parcelFileDescriptor = Base.activity.getContentResolver().openFileDescriptor(uri, "r");
      FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
      copyBitmap(BitmapFactory.decodeFileDescriptor(fileDescriptor));
      parcelFileDescriptor.close();
      parcelFileDescriptor = null;

      if (_Source != null) {
        _BitmapRect = new Rect(0, 0, _Source.getWidth(), _Source.getHeight());
      }
    } catch (FileNotFoundException fnfE) {
      return false;
    } catch (IOException ioE) {
      return false;
    } catch (Exception e) {
      return false;
    }

    if (parcelFileDescriptor != null) {
      try {
        parcelFileDescriptor.close();
      } catch (IOException e) {
        return false;
      }
    }
    return true;
  }
Пример #2
0
 private static Bitmap getArtworkFromFile(Context context, long l, long l1) {
   Bitmap bitmap;
   bitmap = null;
   if (l1 < 0L && l < 0L) throw new IllegalArgumentException("Must specify an album or a song id");
   if (l1 >= 0L) {
     Uri uri1 =
         Uri.parse(
             (new StringBuilder("content://media/external/audio/media/"))
                 .append(l)
                 .append("/albumart")
                 .toString());
     try {
       ParcelFileDescriptor parcelfiledescriptor1 =
           context.getContentResolver().openFileDescriptor(uri1, "r");
       bitmap = null;
       if (parcelfiledescriptor1 != null) {
         bitmap = BitmapFactory.decodeFileDescriptor(parcelfiledescriptor1.getFileDescriptor());
         parcelfiledescriptor1.close();
       }
       Uri uri = ContentUris.withAppendedId(sArtworkUri, l1);
       ParcelFileDescriptor parcelfiledescriptor =
           context.getContentResolver().openFileDescriptor(uri, "r");
       bitmap = null;
       if (parcelfiledescriptor != null) {
         bitmap = BitmapFactory.decodeFileDescriptor(parcelfiledescriptor.getFileDescriptor());
         parcelfiledescriptor.close();
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return bitmap;
 }
    public boolean mkdir() throws IOException {
      if (file.exists()) {
        return file.isDirectory();
      }

      File tmpFile = new File(file, ".MediaWriteTemp");
      int albumId = getTemporaryAlbumId();

      if (albumId == 0) {
        throw new IOException("Fail");
      }

      Uri albumUri = Uri.parse(ALBUM_ART_URI + '/' + albumId);
      ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, tmpFile.getAbsolutePath());

      if (contentResolver.update(albumUri, values, null, null) == 0) {
        values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
        contentResolver.insert(Uri.parse(ALBUM_ART_URI), values);
      }

      try {
        ParcelFileDescriptor fd = contentResolver.openFileDescriptor(albumUri, "r");
        fd.close();
      } finally {
        MediaFile tmpMediaFile = new MediaFile(context, tmpFile);
        tmpMediaFile.delete();
      }

      return file.exists();
    }
 private void closeAccessory() {
   readTh.interrupt();
   try {
     Thread.sleep(50);
   } catch (InterruptedException e1) {
     e1.printStackTrace();
   }
   try {
     if (mFileDescriptor != null) {
       mFileDescriptor.close();
     }
   } catch (IOException e) {
   }
   try {
     if (mInputStream != null) {
       mInputStream.close();
     }
   } catch (IOException e) {
   }
   try {
     if (mOutputStream != null) {
       mOutputStream.close();
     }
   } catch (IOException e) {
   }
   mFileDescriptor = null;
   mOutputStream = null;
   mInputStream = null;
   mAccessory = null;
   avalible = false;
   showExitNotification();
 }
Пример #5
0
  public static Bitmap getArtwork(Context context, long songId, long albumId, int targetSize) {
    if (albumId < 0 && songId < 0) {
      return null;
    }

    Bitmap bmp = null;
    try {
      ParcelFileDescriptor pfd = null;
      if (albumId < 0) {
        Uri uri = Uri.parse("content://media/external/audio/media/" + songId + "/albumart");
        pfd = context.getContentResolver().openFileDescriptor(uri, "r");
      } else {
        Uri uri = ContentUris.withAppendedId(AlbumArtUri, albumId);
        pfd = context.getContentResolver().openFileDescriptor(uri, "r");
      }
      if (pfd == null) {
        return null;
      }

      bmp = decodeImage(pfd.getFileDescriptor(), targetSize);
      pfd.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bmp;
  }
Пример #6
0
  /** Closes the accessory and cleans up all lose ends */
  private void closeAccessory() {

    if (open == false) {
      return;
    }

    open = false;
    enabled = false;
    permissionRequested = false;

    if (readThread != null) {
      readThread.cancel();
    }

    if (outputStream != null) {
      try {
        outputStream.close();
      } catch (IOException e) {
      }
    }

    if (parcelFileDescriptor != null) {
      try {
        parcelFileDescriptor.close();
      } catch (IOException e) {
      }
    }

    outputStream = null;
    // readThread = null;
    parcelFileDescriptor = null;
  }
Пример #7
0
  @Override
  protected void onStop() {
    super.onStop();
    try {
      if (parcelRead != null) {
        parcelRead.close();
        parcelWrite.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    mediaRecorder.stop();
    mediaRecorder.release();
    camera.release();
  }
Пример #8
0
 @Override
 public synchronized void run() {
   try {
     Log.d(TAG, "Starting");
     // If anything needs to be obtained using the network, get it now.
     // This greatly reduces the complexity of seamless handover, which
     // tries to recreate the tunnel without shutting down everything.
     // In this mode, all we need to know is the server address.
     InetSocketAddress server = new InetSocketAddress(serverAddress, Integer.parseInt(serverPort));
     // We try to create the tunnel for several times.
     for (int attempt = 0; attempt < CONNECTION_RETRY_COUNT; ++attempt) {
       handler.sendEmptyMessage(R.string.connecting);
       // Reset the counter if we were connected.
       if (run(server)) {
         attempt = 0;
       }
       // Sleep for a while. This also checks if we got interrupted.
       Thread.sleep(CONNECTION_WAIT_TIMOUT);
     }
     Log.d(TAG, "Giving up");
   } catch (InterruptedException e) {
     Log.e(TAG, "VPN thread interrupted " + e);
   } finally {
     try {
       fileDescriptor.close();
     } catch (IOException e) {
       Log.e(TAG, "FileDescriptor close failed " + e);
     }
     fileDescriptor = null;
     vpnParameters = null;
     handler.sendEmptyMessage(R.string.disconnected);
     Log.d(TAG, "Exiting");
   }
 }
Пример #9
0
 public static boolean mkdir(final Context context, final File file) throws IOException {
   if (file.exists()) {
     return file.isDirectory();
   }
   final File tmpFile = new File(file, ".MediaWriteTemp");
   final int albumId = getTemporaryAlbumId(context);
   if (albumId == 0) {
     throw new IOException("Failed to create temporary album id.");
   }
   final Uri albumUri = Uri.parse(String.format(Locale.US, ALBUM_ART_URI + "/%d", albumId));
   final ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, tmpFile.getAbsolutePath());
   final ContentResolver contentResolver = context.getContentResolver();
   if (contentResolver.update(albumUri, values, null, null) == 0) {
     values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
     contentResolver.insert(Uri.parse(ALBUM_ART_URI), values);
   }
   try {
     final ParcelFileDescriptor fd = contentResolver.openFileDescriptor(albumUri, "r");
     fd.close();
   } finally {
     delete(context, tmpFile);
   }
   return file.exists();
 }
 /** {@inheritDoc} */
 @Override
 public void close() throws IOException {
   try {
     mFd.close();
   } finally {
     super.close();
   }
 }
 /**
  * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources.
  *
  * @throws java.io.IOException When the PDF file cannot be closed.
  */
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private void closeRenderer() throws IOException {
   if (null != mCurrentPage) {
     mCurrentPage.close();
   }
   mPdfRenderer.close();
   mFileDescriptor.close();
 }
Пример #12
0
 private void stopVPN(ParcelFileDescriptor pfd) {
   Log.i(TAG, "Stopping");
   try {
     pfd.close();
   } catch (IOException ex) {
     Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
   }
 }
Пример #13
0
 private static void closeSilently(ParcelFileDescriptor c) {
   if (c == null) return;
   try {
     c.close();
   } catch (Throwable t) {
     // do nothing
   }
 }
Пример #14
0
 private void stopVpn() {
   try {
     tunPFD.close();
   } catch (IOException e) {
     LogUtils.e("failed to stop tunPFD", e);
   }
   tunPFD = null;
   MainActivity.setShouldExit();
   ExitService.execute(this);
 }
 /**
  * {@inheritDoc} If {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set in flags, the file
  * descriptor will be closed after a copy is written to the Parcel.
  */
 public void writeToParcel(Parcel out, int flags) {
   out.writeFileDescriptor(mFileDescriptor);
   if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) {
     try {
       close();
     } catch (IOException e) {
       // Empty
     }
   }
 }
 @Override
 protected void finalize() throws Throwable {
   try {
     if (!mClosed) {
       close();
     }
   } finally {
     super.finalize();
   }
 }
Пример #17
0
 /**
  * Close silently.
  *
  * @param c the c
  */
 private void closeSilently(ParcelFileDescriptor c) {
   Logger.v(TAG, "closeSilently entry");
   if (c == null) {
     return;
   }
   try {
     c.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  /**
   * Calls the base classes close() then calls the closed listener. The listener will be called
   * regardless of whether an IO exception occurs.
   *
   * @author Ian Copland
   * @throws IOException - Will be thrown if the base class throws during close().
   */
  @Override
  public void close() throws IOException {
    try {
      super.close();
    } catch (Exception e) {
      m_listener.onClosed();
      throw new IOException(e);
    }

    m_listener.onClosed();
  }
Пример #19
0
 private void closeAccessory() {
   try {
     if (mFileDescriptor != null) {
       mFileDescriptor.close();
     }
   } catch (IOException e) {
   } finally {
     mFileDescriptor = null;
     mAccessory = null;
   }
 }
Пример #20
0
    public void cancel() {
      continueRunning = false;
      try {
        inputStream.close();
      } catch (IOException e) {
      }

      try {
        myparcelFileDescriptor.close();
      } catch (IOException e) {
      }
    }
Пример #21
0
  // Get album art for specified album. This method will not try to
  // fall back to getting artwork directly from the file, nor will
  // it attempt to repair the database.
  private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
    // used to display this drawable. Take it into account now, so we don't have to
    // scale later.
    w -= 1;
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
      ParcelFileDescriptor fd = null;
      try {
        fd = res.openFileDescriptor(uri, "r");
        int sampleSize = 1;

        // Compute the closest power-of-two scale factor
        // and pass that to sBitmapOptionsCache.inSampleSize, which will
        // result in faster decoding and better quality
        sBitmapOptionsCache.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
        int nextWidth = sBitmapOptionsCache.outWidth >> 1;
        int nextHeight = sBitmapOptionsCache.outHeight >> 1;
        while (nextWidth > w && nextHeight > h) {
          sampleSize <<= 1;
          nextWidth >>= 1;
          nextHeight >>= 1;
        }

        sBitmapOptionsCache.inSampleSize = sampleSize;
        sBitmapOptionsCache.inJustDecodeBounds = false;
        Bitmap b =
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

        if (b != null) {
          // finally rescale to exactly the size we need
          if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
            Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
            // Bitmap.createScaledBitmap() can return the same bitmap
            if (tmp != b) b.recycle();
            b = tmp;
          }
        }

        return b;
      } catch (FileNotFoundException e) {
      } finally {
        try {
          if (fd != null) fd.close();
        } catch (IOException e) {
        }
      }
    }
    return null;
  }
Пример #22
0
 public void release()
 {
     if (zzabb)
     {
         break MISSING_BLOCK_LABEL_14;
     }
     zzEo.close();
     return;
     IOException ioexception;
     ioexception;
     Log.w("BitmapTeleporter", "Could not close PFD", ioexception);
     return;
 }
 /**
  * {@collect.stats} {@description.open} Close the ParcelFileDescriptor. This implementation closes
  * the underlying OS resources allocated to represent this stream. {@description.close}
  *
  * <p>{@property.open runtime formal:java.io.Closeable_MultipleClose}
  *
  * <p>Although only the first call has any effect, it is safe to call close multiple times on the
  * same object. This is more lenient than the overridden {@code AutoCloseable.close()}, which may
  * be called at most once. {@property.close}
  *
  * @throws IOException If an error occurs attempting to close this ParcelFileDescriptor.
  */
 public void close() throws IOException {
   synchronized (this) {
     if (mClosed) return;
     mClosed = true;
   }
   if (mParcelDescriptor != null) {
     // If this is a proxy to another file descriptor, just call through to its
     // close method.
     mParcelDescriptor.close();
   } else {
     Parcel.closeFileDescriptor(mFileDescriptor);
   }
 }
Пример #24
0
  /** TODO */
  private void closeAccessory() {
    accessoryAttached(false);

    try {
      if (mFileDescriptor != null) {
        mFileDescriptor.close();
      }
    } catch (IOException e) {
      Log.e(TAG, "Error closing file", e);
    } finally {
      mFileDescriptor = null;
      mAccessory = null;
    }
  }
  @Override
  protected Bitmap doInBackground(final Void... pParams) {
    try {
      ParcelFileDescriptor parcelFileDescriptor =
          mContext.getContentResolver().openFileDescriptor(mUri, "r");
      FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
      Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
      parcelFileDescriptor.close();
      return image;

    } catch (Exception ex) {
      return null;
    }
  }
Пример #26
0
 public static Bitmap getBitmapFromUri(Uri uri, Context context) throws IOException {
   ParcelFileDescriptor parcelFileDescriptor =
       context.getContentResolver().openFileDescriptor(uri, "r");
   FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
   Bitmap image = null;
   try {
     image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
   } catch (Exception e) {
     Log.e("BitmapFromUri", e.getMessage());
     e.printStackTrace();
   }
   parcelFileDescriptor.close();
   return image;
 }
Пример #27
0
  public static boolean isUriValid(Uri uri, ContentResolver resolver) {
    if (uri == null) return false;

    try {
      ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
      if (pfd == null) {
        Log.e(TAG, "Fail to open URI. URI=" + uri);
        return false;
      }
      pfd.close();
    } catch (IOException ex) {
      return false;
    }
    return true;
  }
Пример #28
0
 // from Parcelable
 @Override
 public void writeToParcel(Parcel out, int flags) {
   if (mRelroFd >= 0) {
     out.writeLong(mLoadAddress);
     out.writeLong(mLoadSize);
     out.writeLong(mRelroStart);
     out.writeLong(mRelroSize);
     try {
       ParcelFileDescriptor fd = ParcelFileDescriptor.fromFd(mRelroFd);
       fd.writeToParcel(out, 0);
       fd.close();
     } catch (java.io.IOException e) {
       Log.e(TAG, "Cant' write LibInfo file descriptor to parcel", e);
     }
   }
 }
Пример #29
0
  private void writeFileContent(Uri uri) {
    try {
      ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(uri, "w");

      FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

      String textContent = textView.getText().toString();

      fileOutputStream.write(textContent.getBytes());

      fileOutputStream.close();
      pfd.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #30
0
  private void configure(String parameters) {
    if (fileDescriptor != null && parameters.equals(vpnParameters)) {
      Log.d(TAG, "Using the previous interface");
      return;
    }
    // Configure a builder while parsing the parameters.
    Builder builder = new Builder();

    for (String parameter : parameters.split(" ")) {
      String[] fields = parameter.split(",");
      switch (fields[0].charAt(0)) {
        case MTU:
          builder.setMtu(Short.parseShort(fields[1]));
          break;
        case ADDRESS:
          builder.addAddress(fields[1], Integer.parseInt(fields[2]));
          break;
        case ROUTE:
          builder.addRoute(fields[1], Integer.parseInt(fields[2]));
          break;
        case DNS:
          builder.addDnsServer(fields[1]);
          break;
        case SEARCH_DOMAIN:
          builder.addSearchDomain(fields[1]);
          break;
        default:
          break;
      }
    }
    // Close the old interface since the parameters have been changed.
    try {
      fileDescriptor.close();
    } catch (IOException e) {
      Log.e(TAG, "File descriptor is already closed " + e);
    }
    // Create a new interface using the builder and save the parameters.
    fileDescriptor =
        builder.setSession(serverAddress).setConfigureIntent(configureIntent).establish();
    vpnParameters = parameters;
    Log.d(TAG, "New interface: " + parameters);
  }