2016-03-04 96 views
0

我在Android定製的攝像頭應用程序工作時,我點擊圖片庫中看到它顯示的是移動默認畫廊和展示的照片,我點擊,但我想,當我在畫廊單擊,然後單擊圖像展現在我的自定義查看我怎麼能實現如何顯示在圖像視圖圖像

public void clickedGallery(View view) { 
    if (MyDebug.LOG) 
     Log.d(TAG, "clickedGallery"); 
    //Intent intent = new Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    Uri uri = null; 
    Media media = getLatestMedia(); 
    if (media != null) { 
     uri = media.uri; 
    } 

    if (uri != null) { 
     // check uri exists 
     if (MyDebug.LOG) 
      Log.d(TAG, "found most recent uri: " + uri); 
     try { 
      ContentResolver cr = getContentResolver(); 
      ParcelFileDescriptor pfd = cr.openFileDescriptor(uri, "r"); 
      if (pfd == null) { 
       if (MyDebug.LOG) 
        Log.d(TAG, "uri no longer exists (1): " + uri); 
       uri = null; 
      } 
      pfd.close(); 
     } catch (IOException e) { 
      if (MyDebug.LOG) 
       Log.d(TAG, "uri no longer exists (2): " + uri); 
      uri = null; 
     } 
    } 
    if (uri == null) { 
     uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    } 
    if (!is_test) { 
     // don't do if testing, as unclear how to exit activity to finish test (for testGallery()) 
     if (MyDebug.LOG) 
      Log.d(TAG, "launch uri:" + uri); 
     final String REVIEW_ACTION = "com.android.camera.action.REVIEW"; 
     try { 
      // REVIEW_ACTION means we can view video files without autoplaying 
      Intent intent = new Intent(REVIEW_ACTION, uri); 
      this.startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      if (MyDebug.LOG) 
       Log.d(TAG, "REVIEW_ACTION intent didn't work, try ACTION_VIEW"); 
      Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
      // from http://stackoverflow.com/questions/11073832/no-activity-found-to-handle-intent - needed to fix crash if no gallery app installed 
      //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("blah")); // test 
      if (intent.resolveActivity(getPackageManager()) != null) { 
       this.startActivity(intent); 
      } else { 
       preview.showToast(null, R.string.no_gallery_app); 
      } 
     } 
    } 
} 

回答

0

下面是完整的代碼,你可以修改requestCode無論你在代碼中聲明瞭什麼,你都可以在下一個活動中傳遞Uri或文件的實際位置。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode != RESULT_CANCELED) { 
      if (requestCode == SELECT_FILE && (data != null)) { 
       Uri selectedImageUri = data.getData(); 
       String[] projection = {MediaStore.MediaColumns.DATA}; 
       Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, 
         null); 
       assert cursor != null; 
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
       cursor.moveToFirst(); 

       String selectedImagePath = cursor.getString(column_index); 
       if(selectedImagePath == null) { 
        selectedImagePath = getActualPathFromUri(selectedImageUri); 
       } 
       cursor.close(); 
      } else { 
       super.onActivityResult(requestCode, resultCode, data); 
      } 
     } 
    } 

    private String getActualPathFromUri(Uri selectedImageUri) { 
     Bitmap bitmap = null; 
     try { 
      bitmap = getBitmapFromUri(selectedImageUri); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if(bitmap == null) { 
      return null; 
     } 

     File imageFileFolder = new File(getCacheDir(),"appName"); 
     if(!imageFileFolder.exists()){ 
      imageFileFolder.mkdir(); 
     } 

     FileOutputStream out = null; 

     File imageFileName = new File(imageFileFolder, "appName-" + System.currentTimeMillis() + ".jpg"); 
     try { 
      out = new FileOutputStream(imageFileName); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
      out.flush(); 
      out.close(); 
     } catch (IOException e) { 
      Log.i("Exception", e.getMessage()); 
     } finally { 
      if(out != null) { 
       try { 
        out.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return imageFileName.getAbsolutePath(); 
    } 

    private Bitmap getBitmapFromUri(Uri uri) throws IOException { 
     ParcelFileDescriptor parcelFileDescriptor = 
       getContentResolver().openFileDescriptor(uri, "r"); 
     assert parcelFileDescriptor != null; 
     FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); 
     Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor); 
     parcelFileDescriptor.close(); 
     return image; 
    } 
+0

我在哪裏使用這些代碼? –

+0

是不是使用意圖打開圖庫或相機? –

+0

請看看我的代碼,只有在點擊的時候才顯示手機的defalut照片瀏覽器 –

0

當您從相機後背您拍攝照片或作品庫後您的onActivityResult方法調用。因此重寫你的onActivityResult像下面

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
    //Read your image here and set in your imageView 
} 
    } 

編輯:

設置圖像的圖像視圖

裏面的onActivityResult

if(data.getData()==null){ 
    bitmap = (Bitmap)data.getExtras().get("data"); 
}else{ 
    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()); 
} 

yourImageView.setImageBitmap(img); 
+0

如何設置圖像在此代碼我要顯示在自定義視圖中的圖像我做的一類圖像視圖如何設置圖像在本次活動 –

+0

檢查我的編輯... – Amsheer

+0

感謝,但我想打開圖片時,我點擊圖庫按鈕,打開新的活動,在那裏,我顯示圖像 –