1. 程式人生 > >Android 呼叫系統圖片瀏覽器

Android 呼叫系統圖片瀏覽器

在做各種專案的時候會經常遇到需要呼叫系統圖片檢視器來檢視自己需要檢視的圖片,因為這樣就可以在圖片上實現多點觸控,放大和縮小,並且在提高效率的同時又能有很好的體驗。

下面的程式碼便是呼叫系統圖片檢視器來檢視自己的圖片的關鍵程式碼:

//獲取你選中的是那一張圖片(ID值)

int pos = mGallery.getSelectedItemPosition();

//判斷此ID值是不是-1,及表示有沒有選中圖片,沒有選中圖片為-1,其次為選中

if (pos == AdapterView.INVALID_POSITION)
      return;

//下方是將ImageList集合中的圖片路徑轉換為可供File識別的String資料,
String value = String.valueOf(mImagesList.get(pos).getPicturePath());
File file = new File(value);

//下方是是通過Intent呼叫系統的圖片檢視器的關鍵程式碼
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);