public File getOutputMediaFile() {
File imagesFolder = new File(Environment
.getExternalStorageDirectory() + "/Orecs");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
random = new Random();
seqNo = random.nextInt(1000000);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
photoName = "IMG_" + timeStamp;
File mediaFile;
mediaFile = new File(imagesFolder, "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
File pictureFile;
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
Log.d("data: ", data+"");
maindata = data;
new PostTask().execute();
}
};
private class PostTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
Log.d("In background", "sfdsdfsd");
// pictureFile = getOutputMediaFile();
Log.d("In background", "wrerwe");
pathFile = pictureFile.getPath();
File f = new File(pathFile);
Matrix mat = new Matrix();
Bitmap bmp = BitmapFactory.decodeByteArray(maindata, 0, maindata.length);
Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
bmpPic = ShrinkBitmap(maindata, 800, 700);
int rotateangle =90;
mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);
bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true);
return bmpPic1;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
ivImg.setImageBitmap(result);
}
}
Intent i = new Intent(currentactivity.this, nextact.class);
System.out.println("Main Data: " +pathFile);
i.putExtra("bytedata", pathFile);
startActivity(i);
注意:在 AsyncTask 中,请参见 pathFile 变量。有意将其发送到另一个活动。并在该活动中获取它并转换为位图:
Intent i = getIntent();
pathFile = i.getStringExtra("bytedata");
File f = new File(pathFile);
Matrix mat = new Matrix();
Bitmap bmp;
try {
bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);