为什么我无法从Android Gallery中获取所选图像?

时间:2011-11-01 06:28:52

标签: android android-layout

在我的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();

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

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

3 个答案:

答案 0 :(得分:2)

在onActivityResult中进行。这里的Uri将是所选图像的Uri。

答案 1 :(得分:2)

当你启动ONActivityResult时,你需要编写onActivityResult,你可以像这样得到被调用意图的输出,

@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     
    }
}

答案 2 :(得分: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;

        }
    }

尝试使用此代码。