我在玩圖像,相機和內部存儲時遇到了問題。 所以我只是要改變我從谷歌的代碼,它具有保存圖像到SD卡的功能。所以我改變了代碼一點點這個以下代碼...無法將拍攝的圖像保存到內部存儲Android相機
public class CameraUtility {
private String currentPhotoPath;
private String imageCategoryName;
private ActionBarActivity activity;
private static final int REQUEST_TAKE_PHOTO = 1;
public CameraUtility(String imageCategoryName, ActionBarActivity activity){
this.imageCategoryName = imageCategoryName;
this.activity = activity;
}
public String getCurrentPhotoPath(){
return currentPhotoPath;
}
public String getImageCategoryName(){
return imageCategoryName;
}
public File createImageFile() throws IOException{
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = imageCategoryName + "_" + timeStamp + "_";
File localStorage = activity.getApplicationContext().getFilesDir();
/*
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
localStorage // directory
);
*/
File image = new File(localStorage, imageFileName.concat(".jpg"));
if(!image.exists()){
image.createNewFile();
}
else{
image.delete();
image.createNewFile();
}
Log.d("LOCAL_STORAGE", localStorage.getAbsolutePath());
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(activity.getApplicationContext(),
"Error occurred while creating the File",
Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d("CAPTURED_AT", currentPhotoPath);
Toast.makeText(activity.getApplication().getApplicationContext(),
"Start Taking the Picture",
Toast.LENGTH_SHORT).show();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
activity.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
else{
Toast.makeText(activity.getApplication().getApplicationContext(),
"File was not successfully created",
Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(activity.getApplication().getApplicationContext(),
"Activity package manager is null",
Toast.LENGTH_SHORT).show();
}
}
public void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
activity.sendBroadcast(mediaScanIntent);
}
}
正如你所看到的,在createImageFile()方法,有其註釋掉一些代碼塊,這部分使我的代碼確實省了將圖像轉換爲外部存儲(sdcard)。 所以我想改變的代碼,使相機的活動,代碼保存所拍攝的圖像到內部存儲器中的變化這一個:
File image = new File(localStorage, imageFileName.concat(".jpg"));
if(!image.exists()){
image.createNewFile();
}
else{
image.delete();
image.createNewFile();
}
但是當我嘗試確認圖像的拍攝,相機活動不關閉,就像這幅畫......
即「檢查」圖標按鈕被按下時,不會採取任何行動。 那麼在這裏發生了什麼?
你在哪裏處理onActivityResult .. –
變化getFilesDir到getExternalFilesDir。 – greenapps
我以爲我不需要onActivityResult,因爲當我使用註釋代碼時,相機活動已關閉,但只有未保存的圖像。但我會嘗試 – user3057361