Android 将图像从一个活动发送到另一个活动imageview
在第一个活动中,相机在surfaceview中打开,单击图像时单击捕获按钮,其预览将显示在imageview的下一个活动中。请帮助我 我的第一项活动:Android 将图像从一个活动发送到另一个活动imageview,android,Android,在第一个活动中,相机在surfaceview中打开,单击图像时单击捕获按钮,其预览将显示在imageview的下一个活动中。请帮助我 我的第一项活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pass); /*imgss = (ImageView)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
/*imgss = (ImageView) findViewById(R.id.imagereceieve);
btn = (Button)findViewById(R.id.button1);
Bundle extras = getIntent().getExtras();
String photoPath = extras.getString("photoPath");
File filePath = getFileStreamPath(photoPath);
imgss.setImageBitmap(BitmapFactory.decodeFile(photoPath));*/
Bundle extras = getIntent().getExtras();
byte[] photo = extras.getByteArray("photo");
Bitmap bitmap = decodeByteArray (photo, 0, photo.length);
ImageView imgView = (ImageView)findViewById(R.id.imagereceieve);
imgView.setImageBitmap(bitmap);
公共类CodeActivity扩展活动实现SurfaceHolder.Callback{
Button btn;
ImageView imgss;
public static final int SELECT_PICTURE = 1;
String selectedImagePath;
Context context;
Bitmap image;
Bundle bundle;
Intent intent;
String pictureFile;
String fileName;
private static int RESULT_LOAD_IMAGE = 1;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
//public static final int SELECT_PICTURE = 1;
private Camera mCamera;
//private CameraPreview mPreview;
private PictureCallback mPicture;
private Button capture, switchCamera;
private Context myContext;
//private LinearLayout cameraPreview;
private boolean cameraFront = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code);
myContext = this;
context = this;
//getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn = (Button)findViewById(R.id.button1);
imgss = (ImageView) findViewById(R.id.imageView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);*/
mCamera.takePicture(null, null, myPictureCallback_JPG);
}
});
imgss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//sendImage();
}
});
}
/*PictureCallback myPictureCallback_JPG = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data,0,data.length);
File imageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "surbeyImg");
imageDirectory.mkdirs();
File imgName = null;
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "surbeyImg" + File.separator + imgName+".jpg");
try
{
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(data);
} catch (IOException e)
{
e.printStackTrace();
}
Intent intentImg = new Intent(CodeActivity.this, PassActivity.class);
intentImg.putExtra("img",imgName);
startActivity(intentImg);
}
};*/
/*PictureCallback myPictureCallback_JPG = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
String fileName = "tempIMG.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(data);
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Intent i = new Intent(context, PassActivity.class);
Bundle bundle = new Bundle();
bundle.putByteArray("photo", fileName);
i.putExtras(bundle);
startActivity(i);
}
})*/
})
我的下一项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
/*imgss = (ImageView) findViewById(R.id.imagereceieve);
btn = (Button)findViewById(R.id.button1);
Bundle extras = getIntent().getExtras();
String photoPath = extras.getString("photoPath");
File filePath = getFileStreamPath(photoPath);
imgss.setImageBitmap(BitmapFactory.decodeFile(photoPath));*/
Bundle extras = getIntent().getExtras();
byte[] photo = extras.getByteArray("photo");
Bitmap bitmap = decodeByteArray (photo, 0, photo.length);
ImageView imgView = (ImageView)findViewById(R.id.imagereceieve);
imgView.setImageBitmap(bitmap);
我认为应该首先初始化字节数组。所以在分配之前,像这样更改代码
Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activty);
Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);
}
内部按钮单击添加此代码
Intent i = new Intent(current_activity.this, Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putByteArray("photo", photo); // photo is an byte array you already stored data in it
i.putExtras(bundle);
startActivity(i);
接收下一个类似这样的活动的图像
Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activty);
Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);
}
你试过什么或者至少是谷歌的吗?我已经为它做了一个代码…捕获的图像直接传递给另一个活动…在下一个活动的我的代码中它不会显示任何内容。请发布你的代码并询问你想用它来ddo什么。我的第一个活动…在其中单击图像并传递给另一个活动在下一个活动中,图像不显示在图像视图中显示..请检查我的代码并告诉我…我错在哪里以及如何在图像视图中设置..使用此代码:位图位图=decodeByteArray(photo,0,photo.length);它将返回我刚刚编辑的帖子。我还有一个问题…你能解决我的问题吗?不客气。那别忘了把它标记为答案。是的,请问。