android camera以時間來命名照片文件

當我們拍照時一般都由Android自動設置文件名,而已我們一般都是用時間來命名文件,以防止出現一樣的文件名而覆蓋了原來的文件。

所以有核心代碼爲:

//得到當前系統時間
        Time t=new Time();
        t.setToNow();
        int year=t.year;
        int month=t.month;
        int day=t.monthDay;
        int hour=t.hour;
        int minute=t.minute;
        int second=t.second;
        Log.i(TAG, ""+year+month+day+hour+minute+second);
        String filename=""+year+month+day+hour+minute+second;
        //得到SD卡的路徑也設置文件名
        //這裏可以簡化的寫成imageFilePath=Uri.parse("file:////sdcard/my.jpg");
        /*imageFilePath=Environment.getExternalStorageDirectory()
                .getAbsolutePath()+"/my01.jpg";*/
        imageFilePath=Environment.getExternalStorageDirectory()
                .getAbsolutePath()+"/"+filename+".jpg";
        //創建文件
        File file=new File(imageFilePath);
        //格式化爲Uri
        Uri fileImageFilePath=Uri.fromFile(file);
        view=(ImageView)findViewById(R.id.imageview);
        Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//啓動intent
        //設置到意圖中
        i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileImageFilePath);
        startActivityForResult(i, CAMERA_RESULT);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章