如何在另一个Activity中指定intent是来自相机还是图库

时间:2014-11-01 10:20:33

标签: android android-intent

我对android很新,我在指定Intent是来自第二个活动的相机还是画廊时遇到了问题。这是我的代码:

MainActivity:

public class MainActivity extends ActionBarActivity implements OnClickListener {

private static final int CAMERA_REQUEST = 1888;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    Button cameraButton = (Button) this.findViewById(R.id.cameraButton);
    Button galleryButton = (Button) this.findViewById(R.id.galleryButton);

    //Here we choose to take a new picture
    cameraButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 

        }
    });

    // Here we choose a photo from the gallery
    galleryButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
        }
    }); 


}       

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

    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode)
    {
        case PICK_FROM_CAMERA:
            if (resultCode == Activity.RESULT_OK) 
            {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

                Intent captureIntent = new Intent(MainActivity.this, ImageViewerActivity.class);
                captureIntent.putExtra("data", imageBitmap);
                startActivity(captureIntent);

            }
            break;

        case PICK_FROM_GALLERY:
            if (resultCode == Activity.RESULT_OK)
            {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                Intent intent = new Intent(this, ImageViewerActivity.class);
                intent.putExtra("picture", byteArray);
                startActivity(intent);
            }
            break;
    }
 }

    public String getPath(Uri uri) {

        // just some safety built in 
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
    }

ImageViewerActivity:

public class ImageViewerActivity extends Activity {

public static ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.photo_view);

    imageView = (ImageView)findViewById(R.id.imageView);
    getData();

}

private void getData() {
    // TODO Auto-generated method stub

        if(//image is from camera)
        {
            Intent intent = getIntent();
            Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");
            imageView.setImageBitmap(bitmap);
        }


        if(//image is from gallery)
        {
            Bundle extras = getIntent().getExtras();
            byte[] byteArray = extras.getByteArray("picture");

            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            imageView.setImageBitmap(bmp);      
        }
}

需要任何帮助!非常感谢!

1 个答案:

答案 0 :(得分:0)

在你的演员中,你可以通过" INTENT_FROM"传递另一个字段。并放置一个带有标识符= CAMERA,GALLERY或其他任何内容的字符串。

要附加它,在发送您的意图之前,创建一个包并将其添加到将要发送的意图中,如下所示:

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

您可以在此处找到管理捆绑包的提示。

Passing a Bundle on startActivity()?

我希望这会对你有帮助!