Java Android-ImageView如何获取相机图片?其中resultCode为0,requestCode为2?
获取resultCode=0和requestCode=2时,无法获取带有图片的picViewJava Android-ImageView如何获取相机图片?其中resultCode为0,requestCode为2?,java,android,linux,android-intent,Java,Android,Linux,Android Intent,获取resultCode=0和requestCode=2时,无法获取带有图片的picView import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import andr
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
final int CAMERA_CAPTURE = 1;
final int PIC_CROP = 2;
private Uri picUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button captureBtn = (Button)findViewById(R.id.capture_btn);
captureBtn.setOnClickListener(this);
}
/**
* Click method to handle user pressing button to launch camera
*/
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
/**
* Handle user returning from both capturing and cropping the image
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_CAPTURE){
picUri = data.getData();
performCrop();
}
else if(requestCode == PIC_CROP){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView)findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
} else {
String errorMessage = "Whoops - resultCode return: " + resultCode +" requestCode: " + requestCode;
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
/**
* Helper method to carry out crop operation
*/
private void performCrop(){
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
编辑:(添加如下,其中data.getData()始终为空)
编辑2:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if(requestCode == CAMERA_CAPTURE){
picUri = data.getData();
performCrop();
}
else if(requestCode == PIC_CROP){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView)findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
} else {
String errorMessage = "Whoops - resultCode return: " + resultCode +" requestCode: " + requestCode;
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView)findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
}
如果resultCode为0,则表示调用的活动已取消。他们必须按下“后退”按钮,或返回此活动。更改是错误的。请查看我的编辑,它在通过边界后始终返回Null。是否使用data.getExtras()?它返回了什么?请看我的编辑2:它给了我图片的快照。但我不能直接裁剪它显示图像。我拍摄的图像右侧为绿色,但在快照中左侧为绿色。没有确切的图像显示。我不认为有任何事情,这段代码必须做的图像交换。将多媒体资料中的图像与您的图像一起检查。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if(requestCode == CAMERA_CAPTURE){
picUri = data.getData();
performCrop();
}
else if(requestCode == PIC_CROP){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView)findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
} else {
String errorMessage = "Whoops - resultCode return: " + resultCode +" requestCode: " + requestCode;
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView)findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
}