2012-11-12 84 views
4

我目前有一個標準的圖像選擇(從SD卡)對話框發射過這個意向時,顯示的影像:充分利用SD卡和相機

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 

這將列出所有的應用程序/活動,可以返回圖像(如圖庫)。

在這個相同的標準列表中,我還希望包含一個選項,以啓動攝像頭並返回與其拍攝的圖像。問題是我無法弄清楚如何以非自定義的方式做到這一點(使用自定義佈局,應用程序圖像+標題等構建我自己的對話框)。

的攝像頭活動可以推出這樣的:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
URI pictureUri = Uri.fromFile(new File("dummyPath")); 
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); 

有沒有意圖,讓我選擇從攝影機拍攝的SD卡或圖像?

更新:我已找到解決方案,將仔細檢查並在此後發佈。

+0

能否請您分享的解決方案:下面 的源代碼給? –

回答

4

您可以通過警報對話框顯示選項。

AlertDialog.Builder getImageFrom = new AlertDialog.Builder(MainActivity.this); 
      getImageFrom.setTitle("Select Image"); 
      final CharSequence[] opsChars = {"Take Picture", "Open Gallery"}; 
      getImageFrom.setItems(opsChars, new android.content.DialogInterface.OnClickListener(){ 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if(which == 0){ 
         File file = new File(_path); 
         outputFileUri = Uri.fromFile(file); 

         Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

         startActivityForResult(cameraIntent, 7); 
        }else 
         if(which == 1){ 
          Intent intent = new Intent(); 
          intent.setType("image/*"); 
          intent.setAction(Intent.ACTION_GET_CONTENT); 
          startActivityForResult(Intent.createChooser(intent, 
           "Open Gallery"), 6); 
         } 
        dialog.dismiss(); 
       } 
      }); 


    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 6) { 
      Uri selectedImageUri = data.getData(); 
     pickerImageView.setPadding(0, 0, 0, 0); 
     pickerImageView.setScaleType(ScaleType.FIT_XY); 
     System.gc(); 
      String filepath = getPath(selectedImageUri); 
      File imagefile = new File(filepath); 
      try { 
      FileInputStream fis = new FileInputStream(imagefile); 
      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inPurgeable=true; 
      options.inSampleSize =4; 
      bi= BitmapFactory.decodeStream(fis,null,options); 
      fis.close(); 
      Bitmap bitmapToRecycle = ((BitmapDrawable)pickerImageView.getDrawable()).getBitmap(); 
      bitmapToRecycle.recycle(); 
      pickerImageView.setImageBitmap(bi); 
      pickerImageView.setPadding(0, 0, 0, 0); 
      pickerImageView.setScaleType(ScaleType.FIT_XY); 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      pickImageTextView.setText(""); 
     } 
     else if(requestCode == 7){ 
      Log.i("return", "#####"); 
      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inPurgeable=true; 
      options.inSampleSize =4; 
      //Bitmap photo = BitmapFactory.decodeFile(_path, options); 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      pickerImageView.setImageBitmap(photo); 
      pickerImageView.setPadding(0, 0, 0, 0); 
      pickerImageView.setScaleType(ScaleType.FIT_XY); 

     } 
    } 
} 
+0

這恰恰是我想要避免的情況:創建自定義對話框,它很混亂,需要時間。如果有一個標準(可能更簡單)的解決方案,我想使用它。 – Buffalo

+0

我不覺得它凌亂。我用它並且工作得很好。 – Sharmilee

+0

我試過你的代碼,它似乎沒有工作。拍攝照片後,相機活動就會掛在那裏。我怎樣才能回到我的活動?必須做什麼特別的事情? – Buffalo

-1

您可以將圖像編程方式添加到庫這樣的:

Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 

// mCurrentPhotoPath是通向圖像。

File f = new File(mCurrentPhotoPath); 
Uri contentUri = Uri.fromFile(f); 
media.setData(contentUri); 
this.sendBroadcast(media);