[英]How to pass image captured by camera to next screen in android?
我在android中使用相機應用程序。 我想將字節數據從PictureCallback方法傳遞給另一個活動,並希望在該活動中顯示它。
Camera.PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
如果有人知道,請幫助我。
您可以使用其他功能:
Camera.PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Intent i = new Intent(context, B.class);
Bundle bundle = new Bundle();
bundle.putByteArray("photo", data);
i.putExtra(bundle );
startActivity(i);
}
};
和B活動:
Bundle extras = getIntent().getExtras();
byte[] photo = extras.getByteArray("photo");
要在第二個活動中顯示圖像,必須將byte []轉換為位圖並將其分配給imageView:
Bitmap bitmap = decodeByteArray (photo, 0, photo.length);
ImageView imgView = (ImageView)findViewById(R.id.preview);
imgView.setImageBitmap(bitmap);
我從沒有嘗試過從byte []解碼為位圖..但是您可以在這里找到更多信息。
編輯: @ ss1271的評論是正確的。 根據此答案,似乎有500Kb的限制。 這意味着,如果您的圖片很大,則應保存它,並將引用傳遞給新活動,如下所示:
// A ACTIVITY
Camera.PictureCallback jpegCallback = 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, B.class);
Bundle bundle = new Bundle();
bundle.putExtra("photoPath", fileName);
i.putExtra(bundle);
startActivity(i);
}
};
// B ACTIVITY
Bundle extras = getIntent().getExtras();
String photoPath = extras.getString("photoPath");
File filePath = getFileStreamPath(photoPath);
//And do whatever you want to do with the File
您可以通過putExtra(String name,byte value)將字節(數據)添加到Intent,然后使用該Intent啟動新的Activity。
蒂姆,祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.