2016-02-10 37 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

相关问题