AndroidQ中沙箱如何適配多媒體文件

這篇文章主要為大家展示了“AndroidQ中沙箱如何適配多媒體文件”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“AndroidQ中沙箱如何適配多媒體文件”這篇文章吧。

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括馬鞍山網(wǎng)站建設(shè)、馬鞍山網(wǎng)站制作、馬鞍山網(wǎng)頁制作以及馬鞍山網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,馬鞍山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到馬鞍山省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

綜述

所有內(nèi)容的訪問變化見下圖:

AndroidQ中沙箱如何適配多媒體文件

外部媒體文件的掃描,讀取和寫入

最容易被踩坑的應(yīng)該是,對(duì)外部媒體文件,照片,視頻,圖片的讀取或?qū)懭搿?/p>

掃描

首先是掃描。掃描依然是使用 query MediaStore 的方式。一句話介紹 MediaStore,MediaStore 就是Android系統(tǒng)中的一個(gè)多媒體數(shù)據(jù)庫。代碼如下圖所示,以搜索本地視頻為例子:

protected List<VideoInfo> doInBackground(Void... params) {
  mContentResolver = context.getContentResolver();

  String[] mediaColumns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
      MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE,
      MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE,
      MediaStore.Video.Media.DATE_ADDED, MediaStore.Video.Media.DURATION,
      MediaStore.Video.Media.WIDTH, MediaStore.Video.Media.HEIGHT };

  Cursor mCursor = mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns,
      null, null, MediaStore.Video.Media.DATE_ADDED);


  if (mCursor == null) {
    return null;
  }

  // 注意,DATA 數(shù)據(jù)在 Android Q 以前代表了文件的路徑,但在 Android Q上該路徑無法被訪問,因此沒有意義。
  ixData = mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
  ixMime = mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE);
  // ID 是在 Android Q 上讀取文件的關(guān)鍵字段
  ixId = mCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
  ixSize = mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
  ixTitle = mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);

  allImages = new ArrayList<VideoInfo>();
  mTotalVideoCount = 0;

  mCursor.moveToLast();
  
  while (mCursor.moveToPrevious()) {
    if (addVideo(mCursor) == 0) {
      continue;
    } else if (addVideo(mCursor) == 1) {
      break;
    }
  }

  mCursor.close();
  
  return allImages;
}

既然 data 不可用,就需要知曉 id 的使用方式,首先是使用 id 拼裝出 content uri ,如下所示:

public getRealPath(String id) {
  return MediaStore.Video.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(String.valueOf(id)).build().toString();
}

Image 同理換成 MediaStore.Images。

讀取和寫入

其次,是讀取 content uri。這里需要注意 File file = new File(contentUri); 是無法獲取到文件的。file.exist() 為 false。

那么就產(chǎn)生兩個(gè)問題:1. 如何確定 ContentUri 形式的文件存在 2. 如何讀取或?qū)懭胛募?/p>

首先,對(duì)于 Content Uri 的讀取,必須借助于 ContentResolver。

其次,對(duì)于 1,沒有找到 Google 文檔中提供比較容易的API,只能采用打開 FileDescriptor 是否成功的形式,代碼如下所示:

public boolean isContentUriExists(Context context, Uri uri) {
  if (null == context) {
    return false;
  }
  ContentResolver cr = context.getContentResolver();
  try {
    AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
    if (null == afd) {
      iterator.remove();
    } else {
      try {
        afd.close();
      } catch (IOException e) {
      }
    }
  } catch (FileNotFoundException e) {
    return false;
  }

  return true;
}

這種方法最大的問題即是,對(duì)應(yīng)于一個(gè)同步 I/O 調(diào)用,易造成線程等待。因此,目前對(duì)于 MediaStore 中掃描出來的文件可能不存在的情況,沒有直接的好方法可以解決過濾。

對(duì)于問題 2,如 1 所示,可以借助 Content Uri 從 ContentResolver 里面拿到 AssetFileDescriptor,然后就可以拿到 InputSteam 或 OutputStream,那么接下來的讀取和寫入就非常自然,如下所示:

public static void copy(File src, ParcelFileDescriptor parcelFileDescriptor) throws IOException {
  FileInputStream istream = new FileInputStream(src);
  try {
    FileOutputStream ostream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
    try {
      IOUtil.copy(istream, ostream);
    } finally {
      ostream.close();
    }
  } finally {
    istream.close();
  }
}

public static void copy(ParcelFileDescriptor parcelFileDescriptor, File dst) throws IOException {
  FileInputStream istream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
  try {
    FileOutputStream ostream = new FileOutputStream(dst);
    try {
      IOUtil.copy(istream, ostream);
    } finally {
      ostream.close();
    }
  } finally {
    istream.close();
  }
}
  
  
public static void copy(InputStream ist, OutputStream ost) throws IOException {
  byte[] buffer = new byte[4096];
  int byteCount = 0;
  while ((byteCount = ist.read(buffer)) != -1) { // 循環(huán)從輸入流讀取 buffer字節(jié)
    ost.write(buffer, 0, byteCount);    // 將讀取的輸入流寫入到輸出流
  }
}

保存媒體文件到公共區(qū)域

這里僅以 Video 示例,Image、Downloads 基本類似:

public static Uri insertVideoIntoMediaStore(Context context, String fileName) {
  ContentValues contentValues = new ContentValues();
  contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
  contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
  contentValues.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");

  Uri uri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
  return uri;
}

這里所做的,只是往 MediaStore 里面插入一條新的記錄,MediaStore 會(huì)返回給我們一個(gè)空的 Content Uri,接下來問題就轉(zhuǎn)化為往這個(gè) Content Uri 里面寫入,那么應(yīng)用上一節(jié)所述的代碼即可實(shí)現(xiàn)。

Video 的 Thumbnail 問題

在 Android Q 上已經(jīng)拿不到 Video 的 Thumbnail 路徑了,又由于沒有暴露 Video 的 Thumbnail 的 id ,導(dǎo)致了 Video 的 Thumbnail 只能使用實(shí)時(shí)獲取 Bitmap 的方法,如下所示:

private Bitmap getThumbnail(ContentResolver cr, long videoId) throws Throwable {
  return MediaStore.Video.Thumbnails.getThumbnail(cr, videoId, MediaStore.Video.Thumbnails.MINI_KIND,
      null);
}

可以進(jìn)去看 Android SDK 的實(shí)現(xiàn),其中最關(guān)鍵的部分是:

String column = isVideo ? "video_id=" : "image_id=";
c = cr.query(baseUri, PROJECTION, column + origId, null, null);
if (c != null && c.moveToFirst()) {
  bitmap = getMiniThumbFromFile(c, baseUri, cr, options);
  if (bitmap != null) {
    return bitmap;
  }
}

進(jìn)一步再進(jìn)去看,可以發(fā)現(xiàn)直接就把 Video/Image 文件打開計(jì)算 Thumbnail。

private static Bitmap getMiniThumbFromFile(
    Cursor c, Uri baseUri, ContentResolver cr, BitmapFactory.Options options) {
  Bitmap bitmap = null;
  Uri thumbUri = null;
  try {
    long thumbId = c.getLong(0);
    String filePath = c.getString(1);
    thumbUri = ContentUris.withAppendedId(baseUri, thumbId);
    ParcelFileDescriptor pfdInput = cr.openFileDescriptor(thumbUri, "r");
    bitmap = BitmapFactory.decodeFileDescriptor(
        pfdInput.getFileDescriptor(), null, options);
    pfdInput.close();
  } catch (FileNotFoundException ex) {
    Log.e(TAG, "couldn't open thumbnail " + thumbUri + "; " + ex);
  } catch (IOException ex) {
    Log.e(TAG, "couldn't open thumbnail " + thumbUri + "; " + ex);
  } catch (OutOfMemoryError ex) {
    Log.e(TAG, "failed to allocate memory for thumbnail "
        + thumbUri + "; " + ex);
  }
  return bitmap;
}

這個(gè) API 毫無疑問設(shè)計(jì)的非常不合理,沒有暴露 Thumbnail 的系統(tǒng)緩存給開發(fā)者,造成了每次都要重新I/O 計(jì)算的極大耗時(shí)。強(qiáng)烈呼吁 Android Q 的正式版能修正這個(gè) API 設(shè)計(jì)缺陷。

以上是“AndroidQ中沙箱如何適配多媒體文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁題目:AndroidQ中沙箱如何適配多媒體文件
網(wǎng)頁地址:http://www.muchs.cn/article20/jsosco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、動(dòng)態(tài)網(wǎng)站微信公眾號(hào)、標(biāo)簽優(yōu)化、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)