android相机在相机取消时在外部sd卡文件夹中创建0kb的文件

时间:2014-01-16 10:48:53

标签: android camera

我的代码中有点问题,请帮助我克服这个问题 我有一个捕捉图像和活动的活动保存在app fin文件夹中。点击保存按钮,它将图像保存在文件夹中,但当用户使用相机活动时,则会在文件夹中创建0kb文件,如何避免这种情况

这是我的相机活动相关代码

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);

    }

}

任何帮助都会受到赞赏, 谢谢你:))

1 个答案:

答案 0 :(得分:1)

我通过删除onActivitiResult()中的0大小文件解决了我的问题, 我得到了我想要的但是我不清楚为什么我的代码在cancle操作后创建0大小的文件?

尊重他人,享受编码

@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();

     }
}