camera - 在相机cancle的外部sd卡文件夹中,安卓相机用 0kb 创建文件

  显示原文与译文双语对照的内容

我有一个问题,请帮助我克服这一点,我有一个 Activity的图像&保存在应用程序文件夹。 单击save保存button保存文件夹中的图像,但当用户cancle摄像机 Activity 随后在文件夹中创建 0kb 文件时,如何避免这里错误

下面是我与相机 Activity 相关的代码


public class camera extends Activity {

 private static final int ACTION_TAKE_PHOTO_B = 1;

 private static final String BITMAP_STORAGE_KEY ="viewbitmap";
 private static final String IMAGEVIEW_VISIBILITY_STORAGE_KEY ="imageviewvisibility";

 private ImageView mImageView;
 private Bitmap mImageBitmap;

 private String mCurrentPhotoPath;

 private static final String JPEG_FILE_PREFIX ="IMG_";
 private static final String JPEG_FILE_SUFFIX =".jpg";

 private File getAlbumDir() {

 String path = Environment.getExternalStorageDirectory().toString();
 File filenamedemo = new File(path +"/ImageFolder/");

 String name = String.valueOf(filenamedemo);
 if (Environment.MEDIA_MOUNTED.equals(Environment
. getExternalStorageState())) {

 if (name!= null) {
 if (!filenamedemo.mkdirs()) {
 if (!filenamedemo.exists()) {
 return null;
 }
 }
 }

 } else {

 }

 return filenamedemo;
 }

 private File createImageFile() throws IOException {
//Create an image file name
 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
. format(new Date(0));
 String imageFileName = JPEG_FILE_PREFIX + timeStamp +"_";
 File albumF = getAlbumDir();
 File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,
 albumF);
 return imageF;
 }

 private File setUpPhotoFile() throws IOException {

 File f = createImageFile();
 mCurrentPhotoPath = f.getAbsolutePath();

 return f;
 }

 private void galleryAddPic() {
 Intent mediaScanIntent = new Intent(
"android.intent.action.MEDIA_SCANNER_SCAN_FILE");
 File f = new File(mCurrentPhotoPath);
 Uri contentUri = Uri.fromFile(f);
 mediaScanIntent.setData(contentUri);
 this.sendBroadcast(mediaScanIntent);
 }

 private void dispatchTakePictureIntent() {

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

 File f = null;

 try {
 f = setUpPhotoFile();
 mCurrentPhotoPath = f.getAbsolutePath();
 takePictureIntent
. putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
 } catch (IOException e) {
 e.printStackTrace();
 f = null;
 mCurrentPhotoPath = null;
 }
 startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_B);

 }

 private boolean isDeviceSupportCamera() {
 if (getApplicationContext().getPackageManager().hasSystemFeature(
 PackageManager.FEATURE_CAMERA)) {
//this device has a camera
 return true;
 } else {
//no camera on this device

 return false;
 }
 }

 private void handleBigCameraPhoto() {

 if (mCurrentPhotoPath!= null) {
//setPic();
//galleryAddPic();
 mCurrentPhotoPath = null;

 Intent viewint = new Intent(camera.this, TashPatti.class);
 startActivity(viewint);
 finish();

 }

 }

/** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 dispatchTakePictureIntent();

/*
 * if (Build.VERSION.SDK_INT> = Build.VERSION_CODES.FROYO) {
 * mAlbumStorageDirFactory = new FroyoAlbumDirFactory(); } else {
 * mAlbumStorageDirFactory = new BaseAlbumDirFactory(); }
 */
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == ACTION_TAKE_PHOTO_B) {

 if (resultCode == RESULT_OK) {

 Toast.makeText(this,"picture saved.", Toast.LENGTH_LONG)
. show();
 handleBigCameraPhoto();

 } else if (resultCode == RESULT_CANCELED) {

//User cancelled the video capture
 Toast.makeText(this,"User cancelled the image capturing.",
 Toast.LENGTH_LONG).show();

 } else {

//Video capture failed, advise user
 Toast.makeText(this,"image capture failed.", Toast.LENGTH_LONG)
. show();
 }
 }
 }

//Some lifecycle callbacks so that the image can survive orientation change
 @Override
 protected void onSaveInstanceState(Bundle outState) {
 outState.putParcelable(BITMAP_STORAGE_KEY, mImageBitmap);

 outState.putBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY,
 (mImageBitmap!= null));

 super.onSaveInstanceState(outState);
 }

 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
 mImageBitmap = savedInstanceState.getParcelable(BITMAP_STORAGE_KEY);
 mImageView.setImageBitmap(mImageBitmap);
 mImageView
. setVisibility(savedInstanceState
. getBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY)? ImageView.VISIBLE
 : ImageView.INVISIBLE);

 }

}

任何帮助都将 appreciatd,谢谢你:

时间: 作者:

通过删除 0大小文件i 解决我的问题在 onActivitiResult ( ) 中,我得到了所需的内容,但我为什么不在cancle操作之后创建 0大小的文件?

refrence,享受编码


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == ACTION_TAKE_PHOTO_B) {

 if (resultCode == RESULT_OK) {

 Toast.makeText(this,"picture saved.",
 Toast.LENGTH_LONG).show();
 Bitmap photo = (Bitmap) data.getExtras().get("data"); 

 byte[] byteData = null;
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
 byteData = baos.toByteArray();

//handleBigCameraPhoto();

 } else if (resultCode == RESULT_CANCELED) {



//User cancelled the video capture
 Toast.makeText(this,"User cancelled the image capturing.",
 Toast.LENGTH_LONG).show();

 } else {


//Video capture failed, advise user
 Toast.makeText(this,"image capture failed.",
 Toast.LENGTH_LONG).show();
 }

//this code delete the file if itz size is 0, 0 size occers wheen user cancles the 
//camera activity so to avoid 0 size file in our folder we are deleting it
 File file= filePath;
 Log.i("lengthhh", Long.toString(file.length()));
 if(file.exists() && file.length()==0)
 {
 file.delete();
 }
 Intent viewint=new Intent(camera.this, TashPatti.class);
 startActivity(viewint); finish();

 }
}

作者:
...