簡體   English   中英

Android從片段啟動相機意圖

[英]Android Launching Camera Intent from Fragment

我目前有一個活動,其中包含多個片段,而我位於集合的第三個片段中。

在該片段中,我使用一個Intent啟動Camera或Gallery。 看到代碼:

public Intent getImageIntent() {

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = context.getPackageManager();
    final List<ResolveInfo> 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;
}

之后,onActivityResult執行:

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mProductBitmap = (Bitmap) extras.get("data");
    imgProduct.setImageBitmap(mProductBitmap);
}

其中mProductBitmap是位圖全局變量,而imgProduct是已初始化的ImageView。

出於某種原因,首先:Camera選項強制關閉應用程序,並從實際片段本身獲取空指針,就像片段項又都為空一樣。 第二:圖庫選項(多選一個)不會出錯,但也不顯示圖像。

任何幫助,將不勝感激。 我已經研究了所有可能的響應,但是沒有多少人從不是活動所承載的初始片段的片段中調用意圖。 謝謝!

編輯:發現在onActivityResult之后,有時我的上下文為Null。 如果有人遇到此幫助,將不勝感激。 謝謝。

編輯:下面是通過onActivityResult方法,大多數情況下,應該執行第一個if。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM