我使用自定义相机,我获取图像字节数组数据并传入另一个片段,但不在图像视图中显示图像。我的代码如下。
public class TakeCameraFragment extends Fragment {
private Camera mCamera = null;
private CameraPreview mCameraPreview;
protected static final int MEDIA_TYPE_IMAGE = 0;
static String FilePAth = "";
Button takePicture;
static String base64string = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camerafragment,
container, false);
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
mCameraPreview = new CameraPreview(getActivity(), mCamera);
FrameLayout preview = (FrameLayout) rootView
.findViewById(R.id.camera_preview_fragment);
preview.addView(mCameraPreview);
takePicture = (Button) rootView
.findViewById(R.id.btnTakePicturefragment);
takePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, null, mPictureframent);
}
});
return rootView;
}
public boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
releaseCamera();
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
releaseCamera();
}
return camera;
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraAppFragment");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
FilePAth = mediaStorageDir.getPath() + File.separator + "IMG_fragment_"
+ timeStamp + ".jpg";
Log.v("log", " FilePAth " + FilePAth);
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_fragment_" + timeStamp + ".jpg");
return mediaFile;
}
PictureCallback mPictureframent = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
/*
* Intent returnIntent = new Intent();
* returnIntent.putExtra("data", data); setResult(RESULT_OK,
* returnIntent); finish();
*/
Log.v("log_tag", "data :: " + data);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
SetPictureImageFragment fm2 = new SetPictureImageFragment();
fragmentTransaction.replace(R.id.relative_Camera_fragment, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putByteArray("position", data);
fm2.setArguments(bundle);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
/*
* public void onBackPressed() { /*Intent returnIntent = new Intent();
* returnIntent.putExtra("path", FilePAth); setResult(RESULT_OK,
* returnIntent); finish(); };
*/
}
我使用了takecamerafragment xml :::
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_Camera_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview_fragment"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:layout_alignParentLeft="true"
android:layout_below="@+id/conform" />
<Button
android:id="@+id/btnTakePicturefragment"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_below="@+id/camera_preview_fragment"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:background="@drawable/camera" />
</RelativeLayout>
我使用了第二个片段::
public class SetPictureImageFragment extends Fragment {
ImageView img;
Bundle bundle;
byte[] path;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.create_store);
View view = inflater.inflate(R.layout.capturepicturefragment, null);
Log.v("log_tag","SetPictureImageFragment");
bundle = this.getArguments();
path = bundle.getByteArray("position");
Log.v("log_tag","SetPictureImageFragment ::: Path :: "+path);
img = (ImageView) view.findViewById(R.id.camera_preview_fragment_imageview);
//img.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap b = BitmapFactory.decodeByteArray(path, 0,path.length);
img.setImageBitmap(b);
img.setScaleType(ScaleType.FIT_XY);
return view;
}
}
我使用了SetPictureImageFragment xml :::
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_CameraImageview_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<Button
android:id="@+id/conform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Conform" />
<Button
android:id="@+id/btnTakecanclePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="cancel" />
<ImageView
android:id="@+id/camera_preview_fragment_imageview"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
如何解决?