我试图存储拍摄的照片但是......
那么如何解决这两个问题?
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;
}
答案 0 :(得分:0)
这是我使用的,它的工作原理:
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);
答案 1 :(得分:0)
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))
按照您的意愿随后使用位图