Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> andorid在 Fragment中調用攝像機

andorid在 Fragment中調用攝像機

編輯:關於Android編程

我現在一個activity中有三個multiple fragments ,我現在采集我第三個 fragments信息

在這個fragments 上我想啟動相機或者圖庫,看下面代碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public Intent getImageIntent() { // Camera. final List cameraIntents = new ArrayList(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = context.getPackageManager(); final List listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(packageName); cameraIntents.add(intent); } // Filesystem. final Intent galleryIntent = new Intent(); galleryIntent.setType(image/*); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // Chooser of filesystem options. final Intent chooserIntent = Intent.createChooser(galleryIntent, Select Source); // Add the camera options. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {})); // Calling activity should exeecute: // startActivityForResult(chooserIntent, 1); return chooserIntent; } After that the onActivityResult executes: private void handleSmallCameraPhoto(Intent intent) { Bundle extras = intent.getExtras(); mProductBitmap = (Bitmap) extras.get(data); imgProduct.setImageBitmap(mProductBitmap); }

其中 mProductBitmap 是一個Bitmap類型的全局變量,imgProduct 是一個已經初始化的 ImageView ,

我現在有如下問題:
1、相機選項強制關閉了app.並在fragment 中報錯了nullpointException
2、圖冊選項不報錯,但是不顯示任何圖片
3、有的時候 在執行完onActivityResult 之後Context 為null

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == Activity.RESULT_OK) { handleSmallCameraPhoto(intent); } else { if (requestCode == 1) { InputStream stream = null; if (intent == null) { System.out.println(DATA IS NULL..); } else { try { if (mProductBitmap != null) { mProductBitmap.recycle(); } stream = getActivity().getContentResolver().openInputStream( intent.getData()); mProductBitmap = BitmapFactory.decodeStream(stream); System.out.println(mProductBitmap); System.out.println(Setting image result); imgProduct.setImageBitmap(mProductBitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (stream != null) try { stream.close(); } catch (IOException e2) { e2.printStackTrace(); } } } } }

處理方法

 

你的圖片已經報錯到了 PATH_TO_SAVE 地址中

你需要在你的onActivityResult 方法中做麼做
File file = new File(PATH_TO_SAVE);
Bitmap bmp = BitmapFactory.decodeFile(file.getPath());

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved