从活动

时间:2018-03-05 15:47:39

标签: java android android-fragments

我有一个Activity,当按下按钮时会提示用户输入一个对话框(这是一个片段)。当用户在此对话框/片段上按“确定”时,用户可以通过相机应用程序拍摄照片。应存储此图片并将其传递到新的片段。新片段应该只是图片,并覆盖整个屏幕。

在Activity中调用onActivityResult()来处理对话框中的操作。在这里,我尝试调出仅由图片组成的片段。

AddChainItemFragment addChainItemFragment = new AddChainItemFragment();
Bundle bundle = new Bundle();
// imageUri is the URI of the camera image that was just taken
Bitmap chainItem = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
bundle.putParcelable("ChainItem", chainItem); // add image to bundle
addChainItemFragment.setArguments(bundle); // pass bundle to fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.llHomeActivity, addChainItemFragment); // replace activity's layout (linearlayout) with the new fragment
ft.commit();

当我运行调试器时,我可以看到AddChainItemFragment的onCreateView()方法被明确调用。 Fragment只是一个ImageView,我将该图像设置为从活动传递到包中的图像。我还在onCreateView()中包含了view.bringToFront()。此片段的onCreateView代码位于:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bitmap chainItem = getArguments().getParcelable("ChainItem");
    View view = inflater.inflate(R.layout.fragment_add_chain_item, container, false);
    ivChainItem = (ImageView) view.findViewById(R.id.ivChainItem);
    ivChainItem.setImageBitmap(chainItem);
    view.bringToFront();
    return view;
}

片段的布局:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ivChainItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="myapp.heisenberg.chainmaker.AddChainItemFragment">
</ImageView>

知道图片无法显示的原因吗?

0 个答案:

没有答案