andorid在 Fragment中調用攝像機

來源:互聯網
上載者:User

andorid在 Fragment中調用攝像機

我現在一個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());

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.