Android 如何指定意图是来自其他活动中的摄影机还是库
我对安卓相当陌生,在第二次活动中,我很难确定意图是来自相机还是画廊。这是我的密码: 主要活动:Android 如何指定意图是来自其他活动中的摄影机还是库,android,android-intent,Android,Android Intent,我对安卓相当陌生,在第二次活动中,我很难确定意图是来自相机还是画廊。这是我的密码: 主要活动: 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
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();
}
图像可视性:
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);
}
}
需要任何帮助!非常感谢 在附加功能中,您可以传递另一个带有“INTENT\u FROM”的字段,并放置一个带有标识符的字符串=照相机、多媒体资料或其他内容 要附加它,在发送意图之前,创建一个捆绑包并将其添加到将要发送的意图中,如下所示:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
您可以在此处找到管理捆绑包的提示
我希望这对你有帮助 什么样的问题?请更具体地说明您的问题,以便我们更容易地帮助您。我不知道如何告诉getData()方法图像是来自gallery还是camera。正如你现在看到的,如果句子是空的。当然,如果有比byteArray更好的传递照片意图的方法,我对一切都持开放态度。我只是在某个地方找到了这段代码,并决定尝试一下。