我想把照片藏起来但是..。
那么,我如何解决这两个问题呢?
public void takePicture() {
Toast.makeText(MainActivity.this,"Entra no takePicture",Toast.LENGTH_LONG).show();
Log.d("entra", "Take Picture");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
Log.d("entra","Vai criar file");
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.d("Erro",ex.getMessage());
}
// Continue only if the File was successfully created
if (photoFile != null) {
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(MainActivity.this,"Entra no onActivityResult()",Toast.LENGTH_LONG).show();
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
image_BMP = (Bitmap) extras.get("data");
bitmap.setImageBitmap(image_BMP);
Log.d("Onactivity result()"," Passa aqui");
myphoto= ImageUtility.getBytes(Bitmap.createBitmap(image_BMP));
}
Toast.makeText(MainActivity.this,"sai do onActivityResult()",Toast.LENGTH_LONG).show();
}
public static File createImageFile() throws IOException {
// Create an image file name
Log.d("entra", "Create File");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File pic = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + pic.getAbsolutePath();
Log.d("entra", "Create File");
return pic;
}发布于 2016-02-15 10:18:28
这就是我所用的,它起作用了:
Uri image = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(image,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
imageView.setBitmap(bitmap);在onActivityResult内部使用,而不是:
Bundle extras = data.getExtras();
image_BMP = (Bitmap) extras.get("data");
bitmap.setImageBitmap(image_BMP);
Log.d("Onactivity result()"," Passa aqui");
myphoto= ImageUtility.getBytes(Bitmap.createBitmap(image_BMP));你不必自己创建文件。相机会处理好的。你可以删除这些东西的代码。
编辑:
要传递自定义图像位置,可以在调用startActivityForResult()之前使用它
Uri fileUri = Uri.fromFile(yourFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);发布于 2016-02-15 10:33:04
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));在startActivityforresult之前添加这一行并存储您的路径,因为有时活动重新启动,您将得到一个空指针,
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("imagePath", mCurrentPhotoPath);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file path
mCurrentPhotoPath = savedInstanceState.getString("imagePath");
}然后,您可以从路径中获取文件,也可以上传文件,也可以使用
Bitmap bitmap = BitmapFactory.decodeFile(new File(filePath))以后按你的意愿处理位图。
https://stackoverflow.com/questions/35406539
复制相似问题