如何在图库视图中从选定图像中获取图像数据?

时间:2011-10-25 16:37:57

标签: android image-processing gallery android-intent

我有以下代码列出了手机图库中的所有jpeg。它工作正常,但当我选择一张图片时,我想用所选图像的数据激发图像处理活动的意图。有什么想法吗?

事先感谢马特

Button useGallery = (Button)findViewById(R.id.loadfromgallery);
        useGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
            }}) ;



    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);


                if(yourSelectedImage != null){
                Log.e(TAG,"pic ok");
                }else{
                     Log.e(TAG,"pic not ok");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我是这样做的:)

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

            switch(requestCode) { 
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    InputStream imageStream = null;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                    Intent intent = new Intent(mContext, DisplayUndistortedBitmapFromGalleryActivity.class);
                    intent.setData(selectedImage);
                    startActivity(intent);

                    if(yourSelectedImage != null){
                    Log.e(TAG,"pic ok");
                    }else{
                         Log.e(TAG,"pic not ok");
                    }
                }
            }

public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {

    private static final String TAG = "*********DUBFGActivity";
    private Context mContext = this;
    Uri uri;
    private Bitmap mbitmap = null;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


         uri = getIntent().getData();
        if(uri != null){
            Log.e(TAG, "uri ok");
        }else {
            Log.e(TAG, "uri not ok");
        }


        try {
              mbitmap = Media.getBitmap(getContentResolver(), uri);
             //setMbitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(mbitmap == null){
            Log.e(TAG,"mbitmap is null");
        }else{
            Log.e(TAG,"mbitmap is not null");
        }