我有下一個問題:當我嘗試啟動相機時,我可以拍攝照片,甚至將其保存在sdcard上,但是當我準備在設備上顯示該照片的路徑時,會出現(xiàn)錯誤。我的全局變量是2(我使用了1,但是使用2來確保它是一個奇怪的錯誤更好): private File photofile;private Uri mMakePhotoUri;這是我的入門相機功能:@SuppressLint("SimpleDateFormat")public void farefoto(int num){// For naming the picture SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String n = sdf.format(new Date()); String fotoname = "Immagine-"+ n +".jpg";//Going through files and folders File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File photostorage2 = new File(photostorage, "Immagini"); System.out.println(photostorage+"\n"+photostorage2); photostorage2.mkdirs();// My file (global) photofile = new File(photostorage2, fotoname); Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera// My URI (global) mMakePhotoUri = Uri.fromFile(photofile); new Bundle(); // I took this code from internet, but if I remove this line, it's the same i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri); startActivityForResult(i, num); //num would be 1 on calling function}和我的活動結果: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (requestCode == 1){ try{ // tring my global URI photo = f.decodeAndResizeFile(new File(mMakePhotoUri.getPath())); } catch(NullPointerException ex){ System.out.println("fail"); ex.printStackTrace(); try{ // Trying my global FILE photo = BitmapFactory.decodeFile(photofile.getAbsolutePath()); } catch (Exception e){ e.printStackTrace(); Toast.makeText(this, "C'è stato un errore. Riprova a scattare la foto.", Toast.LENGTH_LONG).show(); }...................}總是獲取NullPointerException但是... 如果我再拍一張,那就行了?。 N乙呀?jīng)在這里閱讀了所有內(nèi)容...但是在修改全局變量時它沒有邏輯,我無法再使用它...
3 回答

MYYA
TA貢獻1868條經(jīng)驗 獲得超4個贊
正如Alex Cohn所說,我的問題是我onCreate之前打電話的onActivityResult原因是內(nèi)存可能已耗盡(因為有時不這樣做),所以我想讓我的應用“健康”,我嘗試了一些try / catch,因此得到了數(shù)據(jù),即使是正在調(diào)用onCreate還是onActivityResult在第一次調(diào)用時,我都將數(shù)據(jù)寫在Bundle中,如恢復狀態(tài)的鏈接中所述。

12345678_0001
TA貢獻1802條經(jīng)驗 獲得超5個贊
啟動可能會導致ACTION_IMAGE_CAPTURE您的活動耗盡內(nèi)存。您應該檢查(我只是一個日志,調(diào)試器可能會有自己的副作用),onCreate()您的活動是否在before之前被調(diào)用過onActivityResult()。在這種情況下,您應該準備活動以重新初始化自身,可能使用onSaveInstanceState(Bundle)。
請注意,是否要關閉活動或?qū)⑵浔A粼诤笈_的決定取決于您無法控制的整體系統(tǒng)狀態(tài)。如果您拍攝第一張照片時的決定是“把他關下來!”,這不會讓我感到驚訝,但是當您再次拍攝照片時,這是“讓他處于背景中”的決定。
- 3 回答
- 0 關注
- 469 瀏覽
添加回答
舉報
0/150
提交
取消