/** 解析Intent */ private void parseIntent(final Intent intent) { final Bundle arguments = intent.getExtras(); if (arguments != null) { if (arguments.containsKey(EXTRA_DIRECTORY)) { String directory = arguments.getString(EXTRA_DIRECTORY); Logger.i("onStartCommand:" + directory); // 扫描文件夹 if (!mScanMap.containsKey(directory)) mScanMap.put(directory, ""); } else if (arguments.containsKey(EXTRA_FILE_PATH)) { // 单文件 String filePath = arguments.getString(EXTRA_FILE_PATH); Logger.i("onStartCommand:" + filePath); if (!StringUtils.isEmpty(filePath)) { if (!mScanMap.containsKey(filePath)) mScanMap.put(filePath, arguments.getString(EXTRA_MIME_TYPE)); // scanFile(filePath, arguments.getString(EXTRA_MIME_TYPE)); } } } if (mServiceStatus == SCAN_STATUS_NORMAL || mServiceStatus == SCAN_STATUS_END) { new Thread(this).start(); // scan(); } }
/** 保存图片到文件 */ public static boolean saveBitmap(File f, Bitmap bitmap) { if (bitmap == null || bitmap.isRecycled()) return false; FileOutputStream fOut = null; try { if (f.exists()) f.createNewFile(); fOut = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); return true; } catch (FileNotFoundException e) { Logger.e(e); } catch (IOException e) { Logger.e(e); } catch (Exception e) { Logger.e(e); } finally { if (fOut != null) { try { fOut.close(); } catch (IOException e) { Logger.e(e); } } } return false; }
private void openVideo() { if (mUri == null || mPlayer == null) return; mPlayer.reset(); mInitialized = false; mPrepared = false; mVideoSizeKnown = false; try { mPlayer.setScreenOnWhilePlaying(true); mPlayer.setDataSource(PlayerService.this, mUri); // if (mLastAudioTrack != -1) // mPlayer.setInitialAudioTrack(mLastAudioTrack); // if (mLastSubTrackId != -1) // mPlayer.setInitialSubTrack(mLastSubTrackId); if (mSurfaceHolder != null && mSurfaceHolder.getSurface() != null && mSurfaceHolder.getSurface().isValid()) mPlayer.setDisplay(mSurfaceHolder); mPlayer.prepareAsync(); } catch (IllegalArgumentException e) { Logger.e("openVideo", e); } catch (IllegalStateException e) { Logger.e("openVideo", e); } catch (IOException e) { Logger.e("openVideo", e); } }
/** 旋转图片 */ public static Bitmap rotate(Bitmap b, int degrees) { if (degrees != 0 && b != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); try { Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); if (b != b2) { b.recycle(); b = b2; } } catch (OutOfMemoryError ex) { Logger.e(ex); } catch (Exception ex) { Logger.e(ex); } } return b; }
public static Bitmap decodeUriAsBitmap(Context ctx, Uri uri) { Bitmap bitmap = null; try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // options.outWidth = reqWidth; // options.outHeight = reqHeight; BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, options); Logger.i("orgi:" + options.outWidth + "x" + options.outHeight); int be = (int) (options.outHeight / (float) 350); if (be <= 0) be = 1; options.inSampleSize = be; // calculateInSampleSize(options, // reqWidth, reqHeight); Logger.i("inSampleSize:" + options.inSampleSize); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, options); } catch (FileNotFoundException e) { Logger.e(e); } catch (OutOfMemoryError e) { Logger.e(e); } return bitmap; }
/** 提取生成缩略图 */ @SuppressWarnings("unused") private void extractThumbnail(POMedia media) { final Context ctx = OPlayerApplication.getContext(); // ThumbnailUtils. Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(ctx, media.path, ThumbnailUtils.MINI_KIND); try { if (bitmap == null) { // 缩略图创建失败 bitmap = Bitmap.createBitmap( ThumbnailUtils.TARGET_SIZE_MINI_THUMBNAIL_WIDTH, ThumbnailUtils.TARGET_SIZE_MINI_THUMBNAIL_HEIGHT, Bitmap.Config.RGB_565); } media.width = bitmap.getWidth(); media.height = bitmap.getHeight(); // 缩略图 bitmap = ThumbnailUtils.extractThumbnail( bitmap, ConvertUtils.dipToPX(ctx, ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL_WIDTH), ConvertUtils.dipToPX(ctx, ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL_HEIGHT), ThumbnailUtils.OPTIONS_RECYCLE_INPUT); if (bitmap != null) { // 将缩略图存到视频当前路径 File thum = new File(OPlayerApplication.OPLAYER_VIDEO_THUMB, UUID.randomUUID().toString()); media.thumb_path = thum.getAbsolutePath(); // thum.createNewFile(); FileOutputStream iStream = new FileOutputStream(thum); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, iStream); iStream.close(); } // 入库 } catch (Exception ex) { Logger.e(ex); } finally { if (bitmap != null) bitmap.recycle(); } }
/** 扫描 */ private void scan() { // 开始扫描 notifyObservers(SCAN_STATUS_START, null); while (mScanMap.keySet().size() > 0) { String path = ""; for (String key : mScanMap.keySet()) { path = key; break; } if (mScanMap.containsKey(path)) { String mimeType = mScanMap.get(path); if ("".equals(mimeType)) { scanDirectory(path); } else { scanFile(path, mimeType); } // 扫描完成一个 mScanMap.remove(path); } // 任务之间歇息一秒 try { Thread.sleep(1000); } catch (InterruptedException e) { Logger.e(e); } } // 全部扫描完成 notifyObservers(SCAN_STATUS_END, null); // 第一次扫描 OPreference pref = new OPreference(this); if (pref.getBoolean(OPlayerApplication.PREF_KEY_FIRST, true)) pref.putBooleanAndCommit(OPlayerApplication.PREF_KEY_FIRST, false); // 停止服务 stopSelf(); }
/** * 保存入库 * * @throws FileNotFoundException */ private void save(POMedia media) { mDbWhere.put("path", media.path); mDbWhere.put("last_modify_time", media.last_modify_time); // 检测 if (!mDbHelper.exists(media, mDbWhere)) { try { if (media.title != null && media.title.length() > 0) media.title_key = PinyinUtils.chineneToSpell(media.title.charAt(0) + ""); } catch (Exception ex) { Logger.e(ex); } media.last_access_time = System.currentTimeMillis(); // 提取缩略图 // extractThumbnail(media); media.mime_type = FileUtils.getMimeType(media.path); // 入库 mDbHelper.create(media); // 扫描到一个 notifyObservers(SCAN_STATUS_RUNNING, media); } }