java - 使用我的 DicomViewer 在 java 中启动新 Activity 导致 Android Studio 崩溃
问题描述
在 RoomDetailFragment.kt 中,我已经将活动传递给了 java 文件,并尝试使用手机中的 URI“Dicom-path”传递它。没有错误但我不知道如何通过它可能是另一种方式?所以我想intent.putExtra 把它交给我的java 类“dicomimageview.java”。它从onCreate的代码运行并一直工作到最后我的dicomimageview。它在这之后崩溃了,只是给我看了一个红色的矩形。在此之后,我在 dicomimageview 的 onCreate 中添加了新的 Intent。他甚至传递了文件路径,还是只是为了新活动而无法访问我的 dicompath?因为我需要为新活动获取代码
protected void onActivityResult ( int requestCode, int resultCode, Intent data)
. 它将文件转换为图像并可以显示它。但它甚至无法启动活动,所以它只是向我显示了一个白屏,并没有转到 onActivityResult 的下一个活动。我希望有一个人可以帮助我。
更新问题现在他只给我看一张红色图片->我认为这是一个活动问题,但我必须学习如何在 dicomImageViewer.java 中创建一个新活动->仍然失败 在此处输入图像描述
RoomDetailFragment.kt
private fun startOpenFileIntent(action: RoomDetailViewEvents.OpenFile) {
val uri1 = action.uri.toString()
val dicom_ = ".DCM"
if (action.mimeType == MimeTypes.Apk) {
installApk(action)
}
else if (uri1.contains(dicom_)) {
activity?.let {
val intent2 = Intent(it, dicomImageView::class.java).apply {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
setDataAndType(action.uri, dicom_)
}
startActivity(intent2)
}
}
else {
openFile(action)
}
}
dicomImageView.java
public class dicomImageView extends AppCompatActivity {
// First thing: load the Imebra library
private ImageView mImageView; // Used to display the image
private TextView mTextView; // Used to display the patient name
@Override
protected void onCreate(Bundle savedInstanceState) {
System.loadLibrary("imebra_lib");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// We will use the ImageView widget to display the DICOM image
mImageView = findViewById(R.id.imageView);
mTextView = findViewById(R.id.textView);
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data1 = result.getData();
doSomeOperations(data1);
}
});
}
public void doSomeOperations(Intent intent) {
Uri selectedfile = intent.getData();
CodecFactory.setMaximumImageSize(8000, 8000);
InputStream stream = null;
try {
stream = getContentResolver().openInputStream(selectedfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// The usage of the Pipe allows to use also files on Google Drive or other providers
PipeStream imebraPipe = new PipeStream(32000);
// Launch a separate thread that read from the InputStream and pushes the data
// to the Pipe.
Thread pushThread = new Thread(new PushToImebraPipe(imebraPipe, stream));
pushThread.start();
// The CodecFactory will read from the Pipe which is feed by the thread launched
// before. We could just pass a file name to it but this would limit what we
// can read to only local files
DataSet loadDataSet = CodecFactory.load(String.valueOf(intent));
// Get the first frame from the dataset (after the proper modality transforms
// have been applied).
Image dicomImage = loadDataSet.getImageApplyModalityTransform(0);
// Use a DrawBitmap to build a stream of bytes that can be handled by the
// Android Bitmap class.
TransformsChain chain = new TransformsChain();
if (ColorTransformsFactory.isMonochrome(dicomImage.getColorSpace())) {
VOILUT voilut = new VOILUT(VOILUT.getOptimalVOI(dicomImage, 0, 0, dicomImage.getWidth(), dicomImage.getHeight()));
chain.addTransform(voilut);
}
DrawBitmap drawBitmap = new DrawBitmap(chain);
Memory memory = drawBitmap.getBitmap(dicomImage, drawBitmapType_t.drawBitmapRGBA, 4);
// Build the Android Bitmap from the raw bytes returned by DrawBitmap.
Bitmap renderBitmap = Bitmap.createBitmap((int) dicomImage.getWidth(), (int) dicomImage.getHeight(), Bitmap.Config.ARGB_8888);
byte[] memoryByte = new byte[(int) memory.size()];
memory.data(memoryByte);
ByteBuffer byteBuffer = ByteBuffer.wrap(memoryByte);
renderBitmap.copyPixelsFromBuffer(byteBuffer);
// Update the image
mImageView.setImageBitmap(renderBitmap);
mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// Update the text with the patient name
mTextView.setText(loadDataSet.getPatientName(new TagId(0x10, 0x10), 0, new PatientName("Undefined", "", "")).getAlphabeticRepresentation());
}
}
activity_dicom_image_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".features.home.room.detail.dicomImageView">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_margin="0dp"
android:gravity="center_vertical|fill_vertical|center_horizontal|fill_horizontal"
android:orientation="vertical"
android:padding="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="100"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="100">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:adjustViewBounds="false"
android:contentDescription="@string/image_description"
android:cropToPadding="false"
android:padding="0dp"
app:srcCompat="@color/colorprimary" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
同样在应用程序中,在 AndroidManifest.xml 中添加了活动
<application
android:name=".VectorApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Vector.Light"
tools:replace="android:allowBackup">
<activity
android:name=".features.home.room.detail.dicomImageView"
android:exported="true" />
<!-- No limit for screen ratio: avoid black strips -->
<meta-data
解决方案
编辑:
同样在您被调用的活动中,您应该能够在 dicomImageView.java 的 onCreate 中使用 this 从调用活动中获取意图
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Intent intent = getIntent();
...
}
看起来您的语法可能不正确。
尝试改变:
val intent = Intent(context,dicomImageView::class.java)
intent.putExtra("Dicomimage",neu[0])
startActivity(intent)
}
到:
val intent = Intent(context,dicomImageView::class.java).apply {
putExtra("Dicomimage",neu[0])
}
startActivity(intent)
请参阅:https ://developer.android.com/training/basics/firstapp/starting-activity#BuildIntent
推荐阅读
- azure - Connect-AzAccount Powershell 本地
- python - Visual Studio Code 无法启动 Python 交互窗口
- python - 如何将单词中的日期和时间字符串转换为数字?
- cmake - 无法安装最前沿的 CMake 包
- java - 在 Android kotlin 的内部存储中从 Firebase 读取和写入视频
- c - 如何使用带有双指针变量的结构字段(没有支持变量)?
- python - django 表没有名为 Exception 的列
- css - SCSS mixins 中 if/else 条件的语法
- reactjs - 将 Redux 状态重置为未定义或初始状态,除了一种状态
- javascript - 这是加法还是减法?