2015-06-11 82 views
1

我有一個簡單choose image source對話框:活動力摧毀後選擇圖片

public void toggleImagePicker() 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle("Choose Image Source"); 
    builder.setItems(new CharSequence[]{"Gallery", "Camera"}, new DialogInterface.OnClickListener() 
    { 

     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 
      InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
      //    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); 
      switch (which) { 
       case 0: 
        // GET IMAGE FROM THE GALLERY 
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
        intent.setType("image/*"); 

        Intent chooser = Intent.createChooser(intent, "Choose a Picture"); 
        if (mBackgroundClicked) 
         startActivityForResult(chooser, REQUEST_GALLERY_IMAGE_FOR_BACKGROUND); 
        else 
         startActivityForResult(chooser, REQUEST_GALLERY_IMAGE); 
        break; 

       case 1: 
        Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE"); 

        File cameraFolder; 

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
         cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), ".helper/"); 
        else 
         cameraFolder = getActivity().getCacheDir(); 
        if (!cameraFolder.exists()) 
         cameraFolder.mkdirs(); 

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
        String timeStamp = dateFormat.format(new Date()); 
        String imageFileName = "picture_" + timeStamp + ".jpg"; 

        File photo = new File(Environment.getExternalStorageDirectory(), ".helper/" + imageFileName); 
        if (!mBackgroundClicked) 
         mProfilePhotoPath = new String(photo.getAbsolutePath()); 
        else 
         mBackgroundPhotoPath = new String(photo.getAbsolutePath()); 
        getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
        //        initialURI = Uri.fromFile(photo); 
        if (mBackgroundClicked) 
         startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE_FOR_BACKGROUND); 
        else 
         startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE); 

        break; 

       default: 
        break; 
      } 
     } 
    }); 

    builder.show(); 
} 

,我要儘量挑選圖片,我顯示對話框,點擊相機或畫廊。否則,我的活動將被銷燬並重新創建。

LogCat中沒有例外。

一件事情我在日誌中發現:

06-11 14:45:57.700  458-549/? W/InputDispatcher﹕ channel '221e47c0 ******.ProfileExpertActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9 

注:在片段中實現挑選機制。

回答

1

如果沒有應用程序來處理這個動作,那麼在開始與resolveActivity的檢查之前,它會導致NullPointerException檢查是否有任何應用程序來處理它。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
photoPickerIntent.setType("image/*"); 
// Verify that the intent will resolve to an activity 
if (photoPickerIntent.resolveActivity(getPackageManager()) != null) { 
    startActivityForResult(photoPickerIntent, GALLERY_PICKER);  
} 
+0

挑選活動正常啓動。當我選擇圖像時,我的活動重新創建。 –