问候我正在尝试在我的应用中开发一个活动,用户使用相机捕获图像并将其保存到我的图库中的自定义文件夹 这是我正在使用的代码
public class CameraFragment extends Fragment {
private static final String TAG = "testcma";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_PHOTO = 1;
private Camera mCamera;
private SurfaceView mSurfaceView;
private View mProgressContainer;
OutputStream outStream = null;
String fileName;
private Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
// Display the progress indicator
mProgressContainer.setVisibility(View.VISIBLE);
}
};
private Camera.PictureCallback mJpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Create a filename
fileName = System.currentTimeMillis() + ".jpg";
// Save the jpeg data to disk
FileOutputStream os = null;
boolean success = true;
try {
if (isDir()) {
}
/*
* os = getActivity().openFileOutput(fileName,
* Context.MODE_PRIVATE);
*/// os.write(data);
} catch (Exception e) {
Log.e(TAG, "Error writing to file " + fileName, e);
success = false;
} finally {
try {
if (os != null)
os.close();
} catch (Exception e) {
Log.e(TAG, "Error closing file " + fileName, e);
success = false;
}
}
if (success) {
Intent i = new Intent();
i.putExtra(Constants.EXTRA_PHOTO_FILENAME, fileName);
getActivity().setResult(Activity.RESULT_OK, i);
} else {
getActivity().setResult(Activity.RESULT_CANCELED);
}
getActivity().finish();
getActivity().finish();
}
private boolean isDir() {
// TODO Auto-generated method stub
// File file = createImageFile();
final String appDirectoryName = "XYZ";
final File imageRoot = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appDirectoryName);
boolean x = imageRoot.mkdirs();
x = imageRoot.isDirectory();
OutputStream fOut = null;
File imagePath = new File(imageRoot.getAbsolutePath());
File file = new File(imagePath, "GE_" + fileName); // File = new
// File(fileName);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.DESCRIPTION, "descriptoin");
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.ImageColumns.BUCKET_ID, file.toString()
.toLowerCase(Locale.US).hashCode());
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file
.getName().toLowerCase(Locale.US));
values.put("_data", file.getAbsolutePath());
ContentResolver cr = getActivity().getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// final File image = new File(imageRoot,fileName );
return false;
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_pin_camera, container,
false);
mProgressContainer = view
.findViewById(R.id.pin_camera_progressContainer);
mProgressContainer.setVisibility(View.INVISIBLE);
ImageButton takePicButton = (ImageButton) view
.findViewById(R.id.camera_button);
takePicButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.takePicture(mShutterCallback, null, mJpegCallback);
}
}
});
mSurfaceView = (SurfaceView) view
.findViewById(R.id.pin_camera_surfaceView);
SurfaceHolder holder = mSurfaceView.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
// Tell the camera to use this surface as its preview area
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(holder);
}
} catch (IOException exception) {
Log.e(TAG, "Error setting up preview display", exception);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// We can no longer display on this surface, so stop the
// preview.
if (mCamera != null) {
mCamera.stopPreview();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
if (mCamera == null)
return;
// The surface has changed size; update the camera preview size
Camera.Parameters parameters = mCamera.getParameters();
Size s = getBestSupportedSize(
parameters.getSupportedPreviewSizes(), w, h); // To be
// reset
// in
// the
// next
// section
parameters.setPreviewSize(s.width, s.height);
s = getBestSupportedSize(parameters.getSupportedPictureSizes(),
w, h);
parameters.setPictureSize(s.width, s.height);
mCamera.setParameters(parameters);
try {
mCamera.startPreview();
} catch (Exception e) {
Log.e(TAG, "Could not start preview", e);
mCamera.release();
mCamera = null;
}
}
});
//
return view;
}
@TargetApi(20)
@Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
mCamera = Camera.open(0);
} else {
mCamera = Camera.open();
}
}
@Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private Size getBestSupportedSize(List<Size> sizes, int width, int height) {
Size bestSize = sizes.get(0);
int largestArea = bestSize.width * bestSize.height;
for (Size s : sizes) {
int area = s.width * s.height;
if (area > largestArea) {
bestSize = s;
largestArea = area;
}
}
return bestSize;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = System.currentTimeMillis() + "";
fileName = "JPEG_" + timeStamp + "_";
String storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/XYZ";
File dir = new File(storageDir);
dir.mkdir();
File image = new File(storageDir + "/" + fileName + ".jpg");
// Save a file: path for use with ACTION_VIEW intents
fileName = image.getAbsolutePath();
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.DESCRIPTION, "descriptoin");
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.ImageColumns.BUCKET_ID,
image.toString().toLowerCase(Locale.US).hashCode());
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, image.getName()
.toLowerCase(Locale.US));
values.put("_data", image.getAbsolutePath());
ContentResolver cr = getActivity().getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Log.i(TAG, "photo path = " + fileName);
return image;
}
}
它捕获图像并在其中创建文件夹和图像文件,但图像是空白的只是黑色,你可以帮我吗
答案 0 :(得分:0)
您实际上并未将jpeg
缓冲区数据保存到文件中,
您正在isDir
内创建目录,并在其中创建一个带有jpg
扩展名fOut = new FileOutputStream(file);
的文件,但您没有将实际图像内容保存到其中
编辑以下是将数据保存到文件的方法
String fileName = System.currentTimeMillis() + ".jpg";
File directory = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "XYZ");
// Save the jpeg data to disk
boolean success = true;
try {
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new IOException("unable to make directories for path " + directory);
}
File file = new File(directory, "GE_" + fileName);
FileOutputStream fOut = new FileOutputStream(file);
fOut.write(data);
fOut.close();
MediaScannerConnection.scanFile(CameraActivity.this, new String[] {file.getAbsolutePath()},
new String[]{"image/jpg"}, null);
} catch (IOException e) {
e.printStackTrace();
}