2016-02-10 87 views
0

我在Android中有一個PopupWindow,並且裏面有一個imageview。當我按下圖像視圖時,我想打開相機拍攝照片,當我回過頭來爲imageview背景設置照片時。打開相機拍照並將其設置在PopupWindow中的圖像視圖 - android

問題是,當我復出(onActivityResult),則popupwindow是駁回(),並且ImageView的背景是默認值。

ImageView的@的onClick:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (cameraIntent.resolveActivity(getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = createImageFile(); 
       } catch (IOException ex) { 
        // Error occurred while creating the File 
        Log.i(TAG, "IOException"); 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
       } 
      } 

createImageFile()函數

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, // prefix 
      ".jpg",   // suffix 
      storageDir  // directory 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 

    editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
    editor.putString("picture_path", mCurrentPhotoPath); 
    editor.commit(); 

    return image; 
} 

@onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     try { 

      prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
      mCurrentPhotoPath = prefs.getString("picture_path", null); 

      bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)); 
      add_photo.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我用Sharedpreferences避免空上mCurrentPhotoPath

回答

0

假設問題是MediaStore.Images.Media.getBitmap返回null,這是因爲它意味着用於從某些ContentResolver中的某個應用程序處理的URL中檢索圖像,而您的情況並非如此實現。

在您的實現中,您將圖像存儲在設備文件系統上,您可以使用BitmapFactorysdecodeFile方法檢索返回位圖的方法。

+0

我不得不說,我已經測試了我的代碼在三星S5的Android 5.0,它工作。但在其他設備上.. – hackingforgirls

+0

你沒有真正解釋你的問題,所以我假設getBitmap方法返回null,是嗎? –

+0

不! mCurrentPhotoPath爲null,但我使用共享的prefs來保存並重新設置它的值,所以沒關係。它不會更改iMageVie中的圖片,這是我的問題。但在S5上,我測試過了,它正在工作...... headchache .... – hackingforgirls

相關問題