2011-11-01 57 views
0

在我的Android相机应用程序中,我使用此代码从Android图库获取选择图像。为什么我无法从Android图库中获取所选图像?

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

我得到了画廊,一切似乎都很好。但是当我在画廊的特定图像上选择时,没有任何事情发生,画廊的活动完成。

我想选择该图像并对其进行一些操作。 怎么可能?

+0

只是看看这个类似的问题[如何从图库(SD卡)为我的应用程序在Android中选择一个图像?](http://stackoverflow.com/questions/2507898/how-to-pick-a - 图像 - 从画廊-SD卡换我-APP-在-机器人)。 – user370305

回答

2

当你开始你需要写onActivityResult的ActivityForResult,你可以得到被叫意图的输出,像这样,

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {  
     Uri contentUri = data.getData(); 

     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     String tmppath = cursor.getString(column_index); 


     Bitmap croppedImage = BitmapFactory.decodeFile(tmppath); 
     iv.setImageBitmap(croppedImage); //set to your imageview  
    } 
} 
+0

感谢您的回复。现在让我试试看。 –

2

在onActivityResult里面做。这里Uri将是选定图像的Uri。

2
private static final int GALLERY_PICK = 0; 

     /** 
    * Start gallery pick activity. 
    */ 
    protected void startGalleryPickActivity() { 
     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     photoPickerIntent.setType("image/*"); 
     photoPickerIntent.putExtra("crop", "true"); 
     photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri()); 
     photoPickerIntent.putExtra("outputFormat", 
       Bitmap.CompressFormat.JPEG.toString()); 
     startActivityForResult(photoPickerIntent, GALLERY_PICK); 
    } 

    /** 
    * Gets the temp uri. 
    * 
    * @return the temp uri 
    */ 
    private Uri getTempUri() { 
     return Uri.fromFile(getTempFile()); 
    } 

    /** 
    * Gets the temp file. 
    * 
    * @return the temp file 
    */ 
    private File getTempFile() { 
     if (isSDCARDMounted()) { 
      imagePath = Environment.getExternalStorageDirectory() + "/temp.jpg"; 

      File f = new File(Environment.getExternalStorageDirectory(), 
        "temp.jpg"); 
      try { 
       f.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return f; 
     } else { 
      return null; 
     } 
    } 


     protected void onActivityResult(int requestCode, int resultCode, 
      Intent imageReturnedIntent) { 

     switch (requestCode) { 

     case GALLERY_PICK: 

      if (resultCode == RESULT_OK) { 
       //Write your code here 
      } 

      break; 

     default: 
      break; 

     } 
    } 

尝试使用此代码。

+0

感谢您的回复。我会看看。 –