问题描述
我有一个活动 A 启动活动 B。活动 B 是一个导航视图,它在片段 1、2、3 之间循环。片段 3 列出了一组选项卡 a、b、c,这些选项卡通过 ViewPager2 引导到适当的片段和自定义 FragmentStateAdapter。
在片段c中,我有一个BottomSheet,它有一个CropImageView,在xml中布局如下:
edit_bottom_sheet.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/editBottomSheet"
android:background="@color/white"
android:orientation="vertical"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<androidx.core.widget.nestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginHorizontal="10dp">
<ImageView
android:id="@+id/up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_arrow_drop_up_24"/>
<ImageView
android:id="@+id/down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_arrow_drop_down_24"/>
<com.theartofdev.edmodo.cropper.CropImageView
android:id="@+id/cropImageView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/camera"/>
<!--android:layout_weight="1"-->
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:fontFamily="@font/roboto_black"
android:hint="Item Name"/>
<EditText
android:id="@+id/editDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:fontFamily="@font/roboto_light"
android:textSize="12sp"/>
<EditText
android:id="@+id/editPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Price"
android:textSize="12sp"
android:fontFamily="@font/roboto_light"
android:inputType="numberDecimal"/>
<TextView
android:id="@+id/recycleViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<Button
android:id="@+id/bs_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textAllCaps="false"
android:textColor="@color/white"
android:fontFamily="@font/roboto_black"
android:background="@drawable/my_button"
android:textSize="12sp"
android:layout_marginVertical="10dp"/>
</LinearLayout>
</androidx.core.widget.nestedScrollView>
</LinearLayout>
在片段 c 的 OnCreateView 中,我在 CropImageView 上使用了一个 onclicklistener,这样当我单击图像时,它会尝试选择一个自定义图像,然后按如下方式裁剪它:
View root;
LinearLayout editBottomSheetView;
CropImageView cropImageView;
Fragment fragment;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_price,container,false);
editBottomSheetView = root.findViewById(R.id.editBottomSheet);
cropImageView = editBottomSheetView.findViewById(R.id.cropImageView);
fragment = this;
cropImageView.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (CropImage.isExplicitCameraPermissionrequired(getActivity())) {
requestPermissions(new String[]{Manifest.permission.CAMERA},CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
}
else {
CropImage.activity().start(getContext(),fragment);
}
}
});
...
我在片段 c 中也有两个方法处理 onActivityResult:
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
File fileImage = new File(resultUri.getPath());
// compressImage method will compress the image.
byte[] byteImageCompress = compressImage(fileImage);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(byteImageCompress,byteImageCompress.length);
cropImageView.setimageBitmap(bitmapImage);
}
}
}
public byte[] compressImage (File file)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(file);
bos.close();
oos.close();
} catch (IOException e) {
e.printstacktrace();
}
return bos.toByteArray();
}
当我运行程序并获得片段c并打开BottomSheet(edit_bottom_sheet.xml)时,出现了我的第一个问题:我没有看到任何图像(我应该看到@drawable/camera),但是我看到图像应该在的空间,它显示为空白。其他一切都显示正常。我可以使用 ImageView 代替 CropImageView 吗?使用 ImageView,图像显示正确。 ImageView 有 android:adjustViewBounds="true" 而 CropView 没有。当我在另一个活动中查看 CropImageView 时,它显示了一个倾斜的高图像。 ImageView 在片段或活动中正确显示图像,图像比例正确。我使用 CropImageView 的原因是我想裁剪图像并遵循 Android Image Cropper 的文档,它推荐这样做。
当我单击空白的 CropImageView 时,它会询问您是否要选择图像然后正确裁剪。它转到图库视图,选择我要裁剪的图像,单击复选标记,然后转到裁剪图像的位置。这是第二个问题出现的地方,它退出裁剪视图并再次加载片段 c(加载所有以前的活动,但当我打开底部工作表时没有图像)。我想要做的是在 CropImageView 所在的片段 c 屏幕中单击图像时加载裁剪的图像。
我认为正在发生的事情是 onActivityResult 没有激活,因为我在该方法中放置了一个断点并且它从未到达。我在每个 Activity 和 Fragment 的每个 onActivityResult 中放置了断点,但它从未触发它们。
我已经查过如何使用 startPickImageActivity 来做到这一点,但我不知道它在片段中是如何工作的
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)